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

时间:2014-01-03 06:25:33

标签: c# wpf multithreading

我必须根据另一个对象引发的事件更新另一个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中

2 个答案:

答案 0 :(得分:3)

您可以将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

的奖金

答案 1 :(得分:1)

试试这个

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