0

How could I invoke so I won't get the following error?

System.InvalidOperationException:

The calling thread cannot access this object because a different thread owns it.

 // Method 1
                    if (((SolidColorBrush)RRefresh.Fill).Color == CustomGreen.Color && Foldername == string.Empty)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Driver.Navigate().Refresh();
                        }));
                    }

// Method 2
        if (Driver != null && ((SolidColorBrush)RRefresh.Fill).Color == CustomGreen.Color)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                Driver.Navigate().Refresh();
            }));
        }
Sen
  • 5
  • 3

1 Answers1

1

You can make use of control.Dispatcher.CheckAccess() to check whether the current thread owns the control. If it does own it. Otherwise use this method:

this.Dispatcher.Invoke(() =>
{
    ...// your code here.
});
Dungeon
  • 972
  • 2
  • 16
  • 32