从工作线程更新表单控件的最佳方法是什么?

时间:2021-12-13 16:02:52

I've done some research and I can't really find a preferred way to do updating of form controls from a worker thread in C#. I know about the BackgroundWorker component, but what is the best way to do it without using the BackgroundWorker component?

我做了一些研究,我真的找不到从C#中的工作线程更新表单控件的首选方法。我知道BackgroundWorker组件,但是在不使用BackgroundWorker组件的情况下,最好的方法是什么?

4 个解决方案

#1


10  

There's a general rule of thumb that says don't update the UI from any thread other than the UI thread itself. Using the features of the BackgroundWorker is a good idea, but you don't want to and something is happening on a different thread, you should do an "Invoke" or BeginInvoke to force the delegate to execute the method on the UI thread.

一般的经验法则是不要从UI线程本身以外的任何线程更新UI。使用BackgroundWorker的功能是一个好主意,但是您不希望在不同的线程上发生某些事情,您应该执行“Invoke”或BeginInvoke以强制委托在UI线程上执行该方法。

Edit: Jon B made this good point in the comments:

编辑:Jon B在评论中提出了这个好点:

Keep in mind that Invoke() is synchronous and BeginInvoke() is asynchronous. If you use Invoke(), you have to be careful not to cause a deadlock. I would recommend BeginInvoke() unless you really need the call to be synchronous.

请记住,Invoke()是同步的,Be​​ginInvoke()是异步的。如果使用Invoke(),则必须注意不要导致死锁。除非你真的需要调用同步,否则我会推荐BeginInvoke()。

Some simple example code:

一些简单的示例代码:

// Updates the textbox text.
private void UpdateText(string text)
{
  // Set the textbox text.
  m_TextBox.Text = text;
}

public delegate void UpdateTextCallback(string text);

// Then from your thread you can call this...
m_TextBox.Invoke(new UpdateTextCallback(this.UpdateText),
    new object[]{"Text generated on non-UI thread."});

The code above is from a FAQ about it here and a longer more involved one here.

上面的代码来自此处的常见问题解答以及此处涉及的更长的代码。

#2


5  

Why dont you want to do it using the BackgroundWorker? It has a fantastic callback event called ProgressChanged which lets the UI thread know about updates, perfect for progess bar-type updates and the like.

为什么不想使用BackgroundWorker来做?它有一个名为ProgressChanged的精彩回调事件,它允许UI线程了解更新,非常适合于progess条形更新等。

link to details

链接到详细信息

#3


1  

There's a discussion related to this here and one here.

这里有一个与此相关的讨论。

Essentially, you use Invoke to accomplish it.

基本上,您使用Invoke来完成它。

Best of luck!

祝你好运!

#4


1  

I would also consider InvokeRequired (VS2008 only) when calling Invoke. There are times that you will not be updating the UI from a seperate thread. It saves the overhead of creating the delegate etc.

在调用Invoke时我也会考虑InvokeRequired(仅限VS2008)。有时您不会从单独的线程更新UI。它节省了创建委托等的开销。

if (InvokeRequired)
        {
            //This.Invoke added to circumvent cross threading exceptions.
            this.Invoke(new UpdateProgressBarHandler(UpdateProgressBar), new object[] { progressPercentage });
        }
        else
        {
            UpdateProgressBar(progressPercentage);
        }

#1


10  

There's a general rule of thumb that says don't update the UI from any thread other than the UI thread itself. Using the features of the BackgroundWorker is a good idea, but you don't want to and something is happening on a different thread, you should do an "Invoke" or BeginInvoke to force the delegate to execute the method on the UI thread.

一般的经验法则是不要从UI线程本身以外的任何线程更新UI。使用BackgroundWorker的功能是一个好主意,但是您不希望在不同的线程上发生某些事情,您应该执行“Invoke”或BeginInvoke以强制委托在UI线程上执行该方法。

Edit: Jon B made this good point in the comments:

编辑:Jon B在评论中提出了这个好点:

Keep in mind that Invoke() is synchronous and BeginInvoke() is asynchronous. If you use Invoke(), you have to be careful not to cause a deadlock. I would recommend BeginInvoke() unless you really need the call to be synchronous.

请记住,Invoke()是同步的,Be​​ginInvoke()是异步的。如果使用Invoke(),则必须注意不要导致死锁。除非你真的需要调用同步,否则我会推荐BeginInvoke()。

Some simple example code:

一些简单的示例代码:

// Updates the textbox text.
private void UpdateText(string text)
{
  // Set the textbox text.
  m_TextBox.Text = text;
}

public delegate void UpdateTextCallback(string text);

// Then from your thread you can call this...
m_TextBox.Invoke(new UpdateTextCallback(this.UpdateText),
    new object[]{"Text generated on non-UI thread."});

The code above is from a FAQ about it here and a longer more involved one here.

上面的代码来自此处的常见问题解答以及此处涉及的更长的代码。

#2


5  

Why dont you want to do it using the BackgroundWorker? It has a fantastic callback event called ProgressChanged which lets the UI thread know about updates, perfect for progess bar-type updates and the like.

为什么不想使用BackgroundWorker来做?它有一个名为ProgressChanged的精彩回调事件,它允许UI线程了解更新,非常适合于progess条形更新等。

link to details

链接到详细信息

#3


1  

There's a discussion related to this here and one here.

这里有一个与此相关的讨论。

Essentially, you use Invoke to accomplish it.

基本上,您使用Invoke来完成它。

Best of luck!

祝你好运!

#4


1  

I would also consider InvokeRequired (VS2008 only) when calling Invoke. There are times that you will not be updating the UI from a seperate thread. It saves the overhead of creating the delegate etc.

在调用Invoke时我也会考虑InvokeRequired(仅限VS2008)。有时您不会从单独的线程更新UI。它节省了创建委托等的开销。

if (InvokeRequired)
        {
            //This.Invoke added to circumvent cross threading exceptions.
            this.Invoke(new UpdateProgressBarHandler(UpdateProgressBar), new object[] { progressPercentage });
        }
        else
        {
            UpdateProgressBar(progressPercentage);
        }