在另一个线程的eventhandler上调用主线程方法

我必须根据另一个对象引发的事件更新另一个GUI。

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; })); } } 

我必须更新更多的GUI控件。 对于我编写Invoke方法的每个控件。 是否可以调用主GUI,以便我可以通过名称直接控制GUI?
注意:我的应用程序开发是在Visual Studio 2010中

您可以使用SynchronizationContext来实现此目的,从而使生活更轻松。

 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 } 

阅读有关SynchronizationContext的额外奖励

试试这个

 public void PLCMessage_Received(object sender, PLCMessageEventArgs e) { string tempstr = (String)e.Message; App.Current.Dispatcher.Invoke(() => UpdateGUI(tempstr)); //Or BeginInvoke } public void UpdateGUI(string temp) { if (temp == "A1Y101") { lbll2heartbeatack.Content = "Ack-OK"; lbll2heartbeatack.Foreground = Brushes.Green; ; } else if (temp == "A1Y102") { lbll2chargeingscheduleack.Content = "Ack-OK"; lbll2chargeingscheduleack.Foreground = Brushes.Green; ; } else if (temp == "A1Y103") { lbll2productinfo.Content = "Ack-OK"; lbll2productinfo.Foreground = Brushes.Green; } else if (temp == "A1Y104") { lbll2entrytrackingack.Content = "Ack-OK"; lbll2entrytrackingack.Foreground = Brushes.Green; } else if (temp == "A1Y105") { lbll2entrytrackingack.Content = "Ack-OK"; lbll2entrytrackingack.Foreground = Brushes.Green; } else if (temp == "A1Y106") { lbll2entrytrackingack.Content = "Ack-OK"; lbll2entrytrackingack.Foreground = Brushes.Green; } }