如何从另一个线程调用UI方法

时间:2022-08-11 20:58:39

Playing round with Timers. Context: a winforms with two labels.

与计时器一起玩。上下文:带有两个标签的winforms。

I would like to see how System.Timers.Timer works so I've not used the Forms timer. I understand that the form and myTimer will now be running in different threads. Is there an easy way to represent the elapsed time on lblValue in the following form?

我想看看System.Timers.Timer是如何工作的,所以我没有使用Forms计时器。我知道表单和myTimer现在将在不同的线程中运行。有一种简单的方法可以用以下形式表示lblValue上的已用时间吗?

I've looked here on MSDN but is there an easier way !

我在MSDN上看过这里,但有更简单的方法!

Here's the winforms code:

这是winforms代码:

using System.Timers;

namespace Ariport_Parking
{
  public partial class AirportParking : Form
  {
    //instance variables of the form
    System.Timers.Timer myTimer;
    int ElapsedCounter = 0;

    int MaxTime = 5000;
    int elapsedTime = 0;
    static int tickLength = 100;

    public AirportParking()
    {
        InitializeComponent();
        keepingTime();
        lblValue.Text = "hello";
    }

    //method for keeping time
    public void keepingTime() {

        myTimer = new System.Timers.Timer(tickLength); 
        myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);

        myTimer.AutoReset = true;
        myTimer.Enabled = true;

        myTimer.Start();
    }


    void myTimer_Elapsed(Object myObject,EventArgs myEventArgs){

        myTimer.Stop();
        ElapsedCounter += 1;
        elapsedTime += tickLength; 

        if (elapsedTime < MaxTime)
        {
            this.lblElapsedTime.Text = elapsedTime.ToString();

            if (ElapsedCounter % 2 == 0)
                this.lblValue.Text = "hello world";
            else
                this.lblValue.Text = "hello";

            myTimer.Start(); 

        }
        else
        { myTimer.Start(); }

    }
  }
}

5 个解决方案

#1


34  

I guess your code is just a test so I won't discuss about what you do with your timer. The problem here is how to do something with an user interface control inside your timer callback.

我想你的代码只是一个测试,所以我不会讨论你用你的计时器做什么。这里的问题是如何使用计时器回调中的用户界面控件执行某些操作。

Most of Control's methods and properties can be accessed only from the UI thread (in reality they can be accessed only from the thread where you created them but this is another story). This is because each thread has to have its own message loop (GetMessage() filters out messages by thread) then to do something with a Control you have to dispatch a message from your thread to the main thread. In .NET it is easy because every Control inherits a couple of methods for this purpose: Invoke/BeginInvoke/EndInvoke. To know if executing thread must call those methods you have the property InvokeRequired. Just change your code with this to make it works:

大多数Control的方法和属性只能从UI线程访问(实际上它们只能从您创建它们的线程访问,但这是另一个故事)。这是因为每个线程都必须有自己的消息循环(GetMessage()按线程筛选出消息)然后使用Control执行某些操作,您必须将消息从线程分发到主线程。在.NET中它很容易,因为每个Control都为此目的继承了几个方法:Invoke / BeginInvoke / EndInvoke。要知道执行线程是否必须调用那些具有属性InvokeRequired的方法。只需更改您的代码即可使其正常工作:

if (elapsedTime < MaxTime)
{
    this.BeginInvoke(new MethodInvoker(delegate 
    {
        this.lblElapsedTime.Text = elapsedTime.ToString();

        if (ElapsedCounter % 2 == 0)
            this.lblValue.Text = "hello world";
        else
            this.lblValue.Text = "hello";
    }));
}

Please check MSDN for the list of methods you can call from any thread, just as reference you can always call Invalidate, BeginInvoke, EndInvoke, Invoke methods and to read InvokeRequired property. In general this is a common usage pattern (assuming this is an object derived from Control):

请检查MSDN以获取可以从任何线程调用的方法列表,就像您可以随时调用Invalidate,BeginInvoke,EndInvoke,Invoke方法和读取InvokeRequired属性一样。通常,这是一种常见的使用模式(假设这是从Control派生的对象):

void DoStuff() {
    // Has been called from a "wrong" thread?
    if (InvokeRequired) {
        // Dispatch to correct thread, use BeginInvoke if you don't need
        // caller thread until operation completes
        Invoke(new MethodInvoker(DoStuff));
    } else {
        // Do things
    }
}

Note that current thread will block until UI thread completed method execution. This may be an issue if thread's timing is important (do not forget that UI thread may be busy or hung for a little). If you don't need method's return value you may simply replace Invoke with BeginInvoke, for WinForms you don't even need subsequent call to EndInvoke:

请注意,当前线程将阻塞,直到UI线程完成方法执行。如果线程的时间很重要,这可能是一个问题(不要忘记UI线程可能忙或暂停一点)。如果你不需要方法的返回值,你可以简单地将Invoke替换为BeginInvoke,对于WinForms,你甚至不需要后续调用EndInvoke:

void DoStuff() {
    if (InvokeRequired) {
        BeginInvoke(new MethodInvoker(DoStuff));
    } else {
        // Do things
    }
}

If you need return value then you have to deal with usual IAsyncResult interface.

如果您需要返回值,那么您必须处理通常的IAsyncResult接口。

How it works?

A GUI Windows application is based on the window procedure with its message loops. If you write an application in plain C you have something like this:

GUI Windows应用程序基于窗口过程及其消息循环。如果你用普通的C编写一个应用程序,你会有这样的事情:

MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
    TranslateMessage(&message);
    DispatchMessage(&message);
}

With these few lines of code your application wait for a message and then delivers the message to the window procedure. The window procedure is a big switch/case statement where you check the messages (WM_) you know and you process them somehow (you paint the window for WM_PAINT, you quit your application for WM_QUIT and so on).

使用这几行代码,您的应用程序会等待消息,然后将消息传递给窗口过程。窗口过程是一个很大的开关/ case语句,你可以检查你知道的消息(WM_)并以某种方式处理它们(你为WM_PAINT绘制窗口,为WM_QUIT退出应用程序等等)。

Now imagine you have a working thread, how can you call your main thread? Simplest way is using this underlying structure to do the trick. I oversimplify the task but these are the steps:

现在假设你有一个工作线程,你怎么能打电话给你的主线程?最简单的方法是使用这个底层结构来完成这个技巧。我过度简化了任务,但这些步骤是:

  • Create a (thread-safe) queue of functions to invoke (some examples here on SO).
  • 创建一个(线程安全的)函数队列来调用(这里的一些例子在SO上)。
  • Post a custom message to the window procedure. If you make this queue a priority queue then you can even decide priority for these calls (for example a progress notification from a working thread may have a lower priority than an alarm notification).
  • 将自定义消息发布到窗口过程。如果将此队列设为优先级队列,则甚至可以确定这些调用的优先级(例如,来自工作线程的进度通知的优先级可能低于警报通知)。
  • In the window procedure (inside your switch/case statement) you understand that message then you can peek the function to call from the queue and to invoke it.
  • 在窗口过程中(在switch / case语句内),您可以了解该消息,然后您可以查看从队列调用的函数并调用它。

Both WPF and WinForms use this method to deliver (dispatch) a message from a thread to the UI thread. Take a look to this article on MSDN for more details about multiple threads and user interface, WinForms hides a lot of these details and you do not have to take care of them but you may take a look to understand how it works under the hood.

WPF和WinForms都使用此方法将消息从线程传递(分派)到UI线程。有关多线程和用户界面的更多详细信息,请查看MSDN上的这篇文章,WinForms隐藏了很多这些细节,您无需关注它们,但您可能需要了解它是如何工作的。

#2


6  

Personally when I work in a application that works with threads out of the UI one, I usually write this little snippet.

就个人而言,当我在一个与UI中的线程一起工作的应用程序中工作时,我通常会写这个小片段。

private void InvokeUI(Action a)
{
    this.BeginInvoke(new MethodInvoker(a));
}

When i do a async call in a differnt thread I can always callback using.

当我在不同的线程中执行异步调用时,我总是可以使用回调。

InvokeUI(() => { 
   Label1.Text = "Super Cool";
});

Simple and clean.

简单干净。

#3


2  

As asked, here is my answer that checks for cross thread calls, synchronises variable updates, doesen't stop and start the timer and doesn't use the timer for counting elapsed time.

如上所述,这是我的答案,检查跨线程调用,同步变量更新,不停止和启动计时器,并且不使用计时器来计算经过的时间。

EDIT fixed BeginInvoke call. I've done the cross thread invoke using a generic Action, This allows the sender and eventargs to be passed. If these are unused (as they are here) it is more efficient to use MethodInvoker but I suspect the handling would need to be moved into a parameterless method.

编辑修复了BeginInvoke调用。我使用通用Action完成了跨线程调用,这允许传递发送方和eventargs。如果这些是未使用的(因为它们在这里),使用MethodInvoker更有效,但我怀疑需要将处理转移到无参数方法。

public partial class AirportParking : Form
{
    private Timer myTimer = new Timer(100);
    private int elapsedCounter = 0;
    private readonly DateTime startTime = DateTime.Now;

    private const string EvenText = "hello";
    private const string OddText = "hello world";

    public AirportParking()
    {
        lblValue.Text = EvenText;
        myTimer.Elapsed += MyTimerElapsed;
        myTimer.AutoReset = true;
        myTimer.Enabled = true;
        myTimer.Start();
    }

    private void MyTimerElapsed(object sender,EventArgs myEventArgs)
    {
        If (lblValue.InvokeRequired)
        {
            var self = new Action<object, EventArgs>(MyTimerElapsed);
            this.BeginInvoke(self, new [] {sender, myEventArgs});
            return;   
        }

        lock (this)
        {
            lblElapsedTime.Text = DateTime.Now.SubTract(startTime).ToString();
            elapesedCounter++;
            if(elapsedCounter % 2 == 0)
            {
                lblValue.Text = EvenText;
            }
            else
            {
                lblValue.Text = OddText;
            }
        }
    }
}

#4


1  

First, in Windows Forms (and most frameworks), a control can only be accessed (unless documented as "thread safe") by the UI thread.

首先,在Windows窗体(和大多数框架)中,只能通过UI线程访问控件(除非记录为“线程安全”)。

So this.lblElapsedTime.Text = ... in your callback is plain wrong. Take a look at Control.BeginInvoke.

所以你的回调中的this.lblElapsedTime.Text = ...是完全错误的。看一下Control.BeginInvoke。

Second, You should use System.DateTime and System.TimeSpan for your time computations.

其次,您应该使用System.DateTime和System.TimeSpan进行时间计算。

Untested:

未经测试:

DateTime startTime = DateTime.Now;

void myTimer_Elapsed(...) {
  TimeSpan elapsed = DateTime.Now - startTime;
  this.lblElapsedTime.BeginInvoke(delegate() {
    this.lblElapsedTime.Text = elapsed.ToString();
  });
}

#5


0  

Ended up using the following. It's a combination of the suggestions given:

结束使用以下内容。这是给出的建议的组合:

using System.Timers;

namespace Ariport_Parking
{
  public partial class AirportParking : Form
  {

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //instance variables of the form
    System.Timers.Timer myTimer;

    private const string EvenText = "hello";
    private const string OddText = "hello world";

    static int tickLength = 100; 
    static int elapsedCounter;
    private int MaxTime = 5000;
    private TimeSpan elapsedTime; 
    private readonly DateTime startTime = DateTime.Now; 
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    public AirportParking()
    {
        InitializeComponent();
        lblValue.Text = EvenText;
        keepingTime();
    }

    //method for keeping time
    public void keepingTime() {

    using (System.Timers.Timer myTimer = new System.Timers.Timer(tickLength))
    {  
           myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
           myTimer.AutoReset = true;
           myTimer.Enabled = true;
           myTimer.Start(); 
    }  

    }

    private void myTimer_Elapsed(Object myObject,EventArgs myEventArgs){

        elapsedCounter++;
        elapsedTime = DateTime.Now.Subtract(startTime);

        if (elapsedTime.TotalMilliseconds < MaxTime) 
        {
            this.BeginInvoke(new MethodInvoker(delegate
            {
                this.lblElapsedTime.Text = elapsedTime.ToString();

                if (elapsedCounter % 2 == 0)
                    this.lblValue.Text = EvenText;
                else
                    this.lblValue.Text = OddText;
            })); 
        } 
        else {myTimer.Stop();}
      }
  }
}

#1


34  

I guess your code is just a test so I won't discuss about what you do with your timer. The problem here is how to do something with an user interface control inside your timer callback.

我想你的代码只是一个测试,所以我不会讨论你用你的计时器做什么。这里的问题是如何使用计时器回调中的用户界面控件执行某些操作。

Most of Control's methods and properties can be accessed only from the UI thread (in reality they can be accessed only from the thread where you created them but this is another story). This is because each thread has to have its own message loop (GetMessage() filters out messages by thread) then to do something with a Control you have to dispatch a message from your thread to the main thread. In .NET it is easy because every Control inherits a couple of methods for this purpose: Invoke/BeginInvoke/EndInvoke. To know if executing thread must call those methods you have the property InvokeRequired. Just change your code with this to make it works:

大多数Control的方法和属性只能从UI线程访问(实际上它们只能从您创建它们的线程访问,但这是另一个故事)。这是因为每个线程都必须有自己的消息循环(GetMessage()按线程筛选出消息)然后使用Control执行某些操作,您必须将消息从线程分发到主线程。在.NET中它很容易,因为每个Control都为此目的继承了几个方法:Invoke / BeginInvoke / EndInvoke。要知道执行线程是否必须调用那些具有属性InvokeRequired的方法。只需更改您的代码即可使其正常工作:

if (elapsedTime < MaxTime)
{
    this.BeginInvoke(new MethodInvoker(delegate 
    {
        this.lblElapsedTime.Text = elapsedTime.ToString();

        if (ElapsedCounter % 2 == 0)
            this.lblValue.Text = "hello world";
        else
            this.lblValue.Text = "hello";
    }));
}

Please check MSDN for the list of methods you can call from any thread, just as reference you can always call Invalidate, BeginInvoke, EndInvoke, Invoke methods and to read InvokeRequired property. In general this is a common usage pattern (assuming this is an object derived from Control):

请检查MSDN以获取可以从任何线程调用的方法列表,就像您可以随时调用Invalidate,BeginInvoke,EndInvoke,Invoke方法和读取InvokeRequired属性一样。通常,这是一种常见的使用模式(假设这是从Control派生的对象):

void DoStuff() {
    // Has been called from a "wrong" thread?
    if (InvokeRequired) {
        // Dispatch to correct thread, use BeginInvoke if you don't need
        // caller thread until operation completes
        Invoke(new MethodInvoker(DoStuff));
    } else {
        // Do things
    }
}

Note that current thread will block until UI thread completed method execution. This may be an issue if thread's timing is important (do not forget that UI thread may be busy or hung for a little). If you don't need method's return value you may simply replace Invoke with BeginInvoke, for WinForms you don't even need subsequent call to EndInvoke:

请注意,当前线程将阻塞,直到UI线程完成方法执行。如果线程的时间很重要,这可能是一个问题(不要忘记UI线程可能忙或暂停一点)。如果你不需要方法的返回值,你可以简单地将Invoke替换为BeginInvoke,对于WinForms,你甚至不需要后续调用EndInvoke:

void DoStuff() {
    if (InvokeRequired) {
        BeginInvoke(new MethodInvoker(DoStuff));
    } else {
        // Do things
    }
}

If you need return value then you have to deal with usual IAsyncResult interface.

如果您需要返回值,那么您必须处理通常的IAsyncResult接口。

How it works?

A GUI Windows application is based on the window procedure with its message loops. If you write an application in plain C you have something like this:

GUI Windows应用程序基于窗口过程及其消息循环。如果你用普通的C编写一个应用程序,你会有这样的事情:

MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
    TranslateMessage(&message);
    DispatchMessage(&message);
}

With these few lines of code your application wait for a message and then delivers the message to the window procedure. The window procedure is a big switch/case statement where you check the messages (WM_) you know and you process them somehow (you paint the window for WM_PAINT, you quit your application for WM_QUIT and so on).

使用这几行代码,您的应用程序会等待消息,然后将消息传递给窗口过程。窗口过程是一个很大的开关/ case语句,你可以检查你知道的消息(WM_)并以某种方式处理它们(你为WM_PAINT绘制窗口,为WM_QUIT退出应用程序等等)。

Now imagine you have a working thread, how can you call your main thread? Simplest way is using this underlying structure to do the trick. I oversimplify the task but these are the steps:

现在假设你有一个工作线程,你怎么能打电话给你的主线程?最简单的方法是使用这个底层结构来完成这个技巧。我过度简化了任务,但这些步骤是:

  • Create a (thread-safe) queue of functions to invoke (some examples here on SO).
  • 创建一个(线程安全的)函数队列来调用(这里的一些例子在SO上)。
  • Post a custom message to the window procedure. If you make this queue a priority queue then you can even decide priority for these calls (for example a progress notification from a working thread may have a lower priority than an alarm notification).
  • 将自定义消息发布到窗口过程。如果将此队列设为优先级队列,则甚至可以确定这些调用的优先级(例如,来自工作线程的进度通知的优先级可能低于警报通知)。
  • In the window procedure (inside your switch/case statement) you understand that message then you can peek the function to call from the queue and to invoke it.
  • 在窗口过程中(在switch / case语句内),您可以了解该消息,然后您可以查看从队列调用的函数并调用它。

Both WPF and WinForms use this method to deliver (dispatch) a message from a thread to the UI thread. Take a look to this article on MSDN for more details about multiple threads and user interface, WinForms hides a lot of these details and you do not have to take care of them but you may take a look to understand how it works under the hood.

WPF和WinForms都使用此方法将消息从线程传递(分派)到UI线程。有关多线程和用户界面的更多详细信息,请查看MSDN上的这篇文章,WinForms隐藏了很多这些细节,您无需关注它们,但您可能需要了解它是如何工作的。

#2


6  

Personally when I work in a application that works with threads out of the UI one, I usually write this little snippet.

就个人而言,当我在一个与UI中的线程一起工作的应用程序中工作时,我通常会写这个小片段。

private void InvokeUI(Action a)
{
    this.BeginInvoke(new MethodInvoker(a));
}

When i do a async call in a differnt thread I can always callback using.

当我在不同的线程中执行异步调用时,我总是可以使用回调。

InvokeUI(() => { 
   Label1.Text = "Super Cool";
});

Simple and clean.

简单干净。

#3


2  

As asked, here is my answer that checks for cross thread calls, synchronises variable updates, doesen't stop and start the timer and doesn't use the timer for counting elapsed time.

如上所述,这是我的答案,检查跨线程调用,同步变量更新,不停止和启动计时器,并且不使用计时器来计算经过的时间。

EDIT fixed BeginInvoke call. I've done the cross thread invoke using a generic Action, This allows the sender and eventargs to be passed. If these are unused (as they are here) it is more efficient to use MethodInvoker but I suspect the handling would need to be moved into a parameterless method.

编辑修复了BeginInvoke调用。我使用通用Action完成了跨线程调用,这允许传递发送方和eventargs。如果这些是未使用的(因为它们在这里),使用MethodInvoker更有效,但我怀疑需要将处理转移到无参数方法。

public partial class AirportParking : Form
{
    private Timer myTimer = new Timer(100);
    private int elapsedCounter = 0;
    private readonly DateTime startTime = DateTime.Now;

    private const string EvenText = "hello";
    private const string OddText = "hello world";

    public AirportParking()
    {
        lblValue.Text = EvenText;
        myTimer.Elapsed += MyTimerElapsed;
        myTimer.AutoReset = true;
        myTimer.Enabled = true;
        myTimer.Start();
    }

    private void MyTimerElapsed(object sender,EventArgs myEventArgs)
    {
        If (lblValue.InvokeRequired)
        {
            var self = new Action<object, EventArgs>(MyTimerElapsed);
            this.BeginInvoke(self, new [] {sender, myEventArgs});
            return;   
        }

        lock (this)
        {
            lblElapsedTime.Text = DateTime.Now.SubTract(startTime).ToString();
            elapesedCounter++;
            if(elapsedCounter % 2 == 0)
            {
                lblValue.Text = EvenText;
            }
            else
            {
                lblValue.Text = OddText;
            }
        }
    }
}

#4


1  

First, in Windows Forms (and most frameworks), a control can only be accessed (unless documented as "thread safe") by the UI thread.

首先,在Windows窗体(和大多数框架)中,只能通过UI线程访问控件(除非记录为“线程安全”)。

So this.lblElapsedTime.Text = ... in your callback is plain wrong. Take a look at Control.BeginInvoke.

所以你的回调中的this.lblElapsedTime.Text = ...是完全错误的。看一下Control.BeginInvoke。

Second, You should use System.DateTime and System.TimeSpan for your time computations.

其次,您应该使用System.DateTime和System.TimeSpan进行时间计算。

Untested:

未经测试:

DateTime startTime = DateTime.Now;

void myTimer_Elapsed(...) {
  TimeSpan elapsed = DateTime.Now - startTime;
  this.lblElapsedTime.BeginInvoke(delegate() {
    this.lblElapsedTime.Text = elapsed.ToString();
  });
}

#5


0  

Ended up using the following. It's a combination of the suggestions given:

结束使用以下内容。这是给出的建议的组合:

using System.Timers;

namespace Ariport_Parking
{
  public partial class AirportParking : Form
  {

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //instance variables of the form
    System.Timers.Timer myTimer;

    private const string EvenText = "hello";
    private const string OddText = "hello world";

    static int tickLength = 100; 
    static int elapsedCounter;
    private int MaxTime = 5000;
    private TimeSpan elapsedTime; 
    private readonly DateTime startTime = DateTime.Now; 
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    public AirportParking()
    {
        InitializeComponent();
        lblValue.Text = EvenText;
        keepingTime();
    }

    //method for keeping time
    public void keepingTime() {

    using (System.Timers.Timer myTimer = new System.Timers.Timer(tickLength))
    {  
           myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
           myTimer.AutoReset = true;
           myTimer.Enabled = true;
           myTimer.Start(); 
    }  

    }

    private void myTimer_Elapsed(Object myObject,EventArgs myEventArgs){

        elapsedCounter++;
        elapsedTime = DateTime.Now.Subtract(startTime);

        if (elapsedTime.TotalMilliseconds < MaxTime) 
        {
            this.BeginInvoke(new MethodInvoker(delegate
            {
                this.lblElapsedTime.Text = elapsedTime.ToString();

                if (elapsedCounter % 2 == 0)
                    this.lblValue.Text = EvenText;
                else
                    this.lblValue.Text = OddText;
            })); 
        } 
        else {myTimer.Stop();}
      }
  }
}