2

Is there in SilverLight something equivalent to Control.InvokeRequired in Winforms?

I already found that Winforms Invoke is equivalent to Control.Dispatcher.BeginInvoke but I cant find nothing like InvokeRequired

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Wally_the_Walrus
  • 219
  • 1
  • 5
  • 17

1 Answers1

3

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();
}
ahmedsafan86
  • 1,776
  • 1
  • 26
  • 49