Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
978 views
in Technique[技术] by (71.8m points)

c# - Invoke main thread method on eventhandler of another thread

I have to update another GUI based on events raised by another object.

public void PLCMessage_Received(object sender, PLCMessageEventArgs e)
{
   string tempstr = (String)e.Message;
   UpdateGUI(tempstr);
}
public void UpdateGUI(string temp)
{
   if (temp == "A1Y101")
   {
     lbll2heartbeatack.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2heartbeatack.Content = "Ack-OK"; lbll2heartbeatack.Foreground = Brushes.Green; }));
   }
   else if (temp == "A1Y102")
   {
     lbll2chargeingscheduleack.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2chargeingscheduleack.Content = "Ack-OK"; lbll2chargeingscheduleack.Foreground = Brushes.Green; }));
   }
   else if (temp == "A1Y103")
   {
      lbll2productinfo.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2productinfo.Content = "Ack-OK"; lbll2productinfo.Foreground = Brushes.Green; }));
   }
   else if (temp == "A1Y104")
   {
      lbll2entrytrackingack.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2entrytrackingack.Content = "Ack-OK"; lbll2entrytrackingack.Foreground = Brushes.Green; }));
   }
   else if (temp == "A1Y105")
   {
       lbll2exittrackinginfoack.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2exittrackinginfoack.Content = "Ack-OK"; lbll2exittrackinginfoack.Foreground = Brushes.Green; }));
   }
   else if (temp == "A1Y106")
   {
       lbll2rejectreqack.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2rejectreqack.Content = "Ack-OK"; lbll2rejectreqack.Foreground = Brushes.Green; }));
   }
}

There are more number of GUI controls which I have to update. For every control I writing th Invoke method. Is it possible to call on main GUI so that i can GUI controls directly by their name?
Note: My application development is in Visual Studio 2010

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use SynchronizationContext for that purpose, that makes life easier.

private SynchronizationContext uiContext = SynchronizationContext.Current;

public void PLCMessage_Received(object sender, PLCMessageEventArgs e)
{
    string tempstr = (String)e.Message;
    uiContext.Send((x)=> UpdateGUI(tempstr), null);//For synchronous
    //Or
    uiContext.Post((x)=> UpdateGUI(tempstr), null);//For Asynchronous
}

Bonus reading about SynchronizationContext


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...