-3

My code is as,

Ping ping = new Ping();
ping.PingCompleted += ping_PingCompleted;
ping.SendAsync(strTerminalName, 60, Encoding.ASCII.GetBytes("sfk"));

 private void ping_PingCompleted(object sender, PingCompletedEventArgs e)
 {
    Terminal.ChangeTerminalStatus(this.imgCurrent, TerminalStatus.UserOFF);
 }

Terminal has static method ChangeTerminalStatus which i am calling in that ping_PingCompleted.

ERROR i got :-

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

Since i tried to change UI Property of a window in that statis method.

I searched and got solution to use Dispatcher.BeginInvoke method.

When i tried to use this in a class which i has ping , Dispatcher.BeginInvoke it throws error says

Error   2   An object reference is required for the non-static field, method, or property 'System.Windows.Threading.Dispatcher.BeginInvoke(System.Delegate, params object[])'   D:\Net Projects\mercurial\icafemanager\ICMBusiness\Terminal.cs  124 17  ICM

Help me out in this.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
shanmugharaj
  • 3,814
  • 7
  • 42
  • 70
  • 1
    it is not clear what is been asked. please rephrase. – Nahum Dec 04 '13 at 06:41
  • possible duplicate of [An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi) – Spontifixus Mar 19 '14 at 17:50

1 Answers1

5

BeginInvoke is not a static method of the Dispatcher class. You need a dispatcher instance to call it. In your tutorial, the class your were calling it from probably had a member that was named dispatcher, so it could be called as

this.Dispatcher.BeginInvoke(...);

If you need the current Dispatcher, there is a static property CurrentDispatcher to get it:

Dispatcher.CurrentDispatcher.BeginInvoke(...);

If your class already has a dispatcher object associated though, it's probably faster to use that.

nvoigt
  • 75,013
  • 26
  • 93
  • 142