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

C# 在另一个线程的eventhandler上调用主线程方法,c#,wpf,multithreading,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.

我必须根据另一个对象引发的事件更新另一个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控件。对于我编写的每个控件,调用方法。是否可以调用主GUI,以便直接按GUI控件的名称访问它们?
注意:我的应用程序开发是在Visual Studio 2010中进行的,您可以将其用于此目的,这使生活更加轻松

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
}
关于你可以使用的

的额外阅读,使生活更轻松

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
}
关于

的额外阅读试试这个

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;
        }
    }
试试这个

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

@Olivarsham编辑格式时,请注意不要删除答案中的链接。@Amar这不是他的错误,这是由于缺少互斥。我想我们两个都在同时编辑:)我看到这个编辑在审查队列中得到批准。我想我应该向编辑指出这一点,以便他下次处理。@Amar同意你的意见,因为我一看到这一点就有机会回滚,如果不是这样的话,这些链接就会丢失。@Olivarsham请注意在编辑格式时不要删除答案中的链接。@Amar这不是他的错,这是由于缺乏相互排斥。我想我们两个都在同时编辑:)我看到这个编辑在审查队列中得到批准。我想我应该向编辑指出这一点,以便他下次处理。@Amar同意你的观点,因为我一看到这一点,我就有机会回滚,如果不是这样的话,这些链接就会丢失。