The following extension methods are very useful
public static bool InvokeRequired(this FrameworkElement element)
{
return !element.Dispatcher.CheckAccess();
}
public static void Invoke(this FrameworkElement element, Action action)
{
if (element.InvokeRequired())
{
using (AutoResetEvent are = new AutoResetEvent(false))
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
action.Invoke();
are.Set();
});
are.WaitOne();
}
}
else
action.Invoke();
}