WPF Controls has no ‘InvokeRequired’/’Invoke’ like WinForms, so making accessing a WPF Window/Control threadsafe can be accomplished by using the System.Windows.Threading.Dispatcher. Here are some examples. First a property:
public int Progress
{
get
{
if (!Dispatcher.CheckAccess())
{
Func<int> f = delegate () { return Progress; };
return Dispatcher.Invoke(f);
}
...
}
set
{
if (!Dispatcher.CheckAccess())
{
Action<int> a = delegate (int progress) { Progress = progress; };
Dispatcher.Invoke(a);
return;
}
...
}
}
A void method
public void SetProgress(object obj, int progress)
{
if (!Dispatcher.CheckAccess())
{
Action<object, int> a = new Action<object, int>(SetProgress);
Dispatcher.Invoke(a, DispatcherPriority.Normal, obj, progress);
return;
}
...
}
And lastly a method with a return value
public bool AllProcessesDone()
{
if (!Dispatcher.CheckAccess())
{
Func<bool> f = new Func<bool>(AllProcessesDone);
return Dispatcher.Invoke(f, DispatcherPriority.Normal);
}
...
}
As always, feel free to comment, or ask.