I perform a lengthy operation (connection test, validation of remote DB tables, etc.) in a separate thread in a WPF app. During the testing, I collect information for the user about which tests were passed and which weren’t. The information is stored as a List<T> of objects of my own design:
public class StatusItem : DependencyObject
{
public string Text { get... set... }
public Status Status { get... set... }
public string Details { get... set... }
}
All these properties are a front for their corresponding DependencyProperty. When the operation is completed (in the separate thread), I set the collected info, status, to a private field on my Window. I get a:
InvalidOperationException:
The calling thread cannot access this object because a different thread owns it.
Is there a way for me to transfer status (a List<StatusItem>) from TestThread to my main thread without having to resort to delegates and Dispatcher invokes?
PS: I could do Invoke, but I’d rather avoid having to create a CopyStatusItemsDelegate delegate.
I don’t think there is a nice solution. The above situation is the reason, why I don’t derive Item-ViewModels from
DependencyObjectbut implementINotifiyPropertyChanged.