C# 3.0 C#[4.0之前]--如何在回调长度计算后更新GUI

C# 3.0 C#[4.0之前]--如何在回调长度计算后更新GUI,c#-3.0,C# 3.0,我想知道实现以下场景的最佳实践 public class MainWindow : Window { public void MainWorkflow() { // initiate a work-thread to run LongCompute // when LongCompute finishes, update the corresponding GUI // for example, change the statusbar as "Computat

我想知道实现以下场景的最佳实践

public class MainWindow : Window
{

  public void MainWorkflow()
  {
    // initiate a work-thread to run LongCompute
    // when LongCompute finishes, update the corresponding GUI
    // for example, change the statusbar as "Computation is done"
  }

  private void LongCompute()
  {
    // a 2-minute computation and then update member variables 
    // after it finishes. I expect the main thread to use the 
    // updated member variables to update GUI later
  }
}
我正在寻找一个具体的好例子来说明这项任务的最佳实践。 正如我们所知,不应该使用工作线程来更新GUI,因为GUI应该由创建它们的线程来更新。另外,我知道以下两种模式:

案例一>主线程等待工作线程,然后在之后更新GUI。 例如 在这种情况下,我们可以使用AutoResteEvent中定义的WaitOne方法,它将由Set方法触发。但这不是一个好办法


案例二>为工作线程设置回调函数,但是,回调函数仍在工作线程中调用,这不利于操作GUI。

正如@Jeff在评论中所说,请使用BackgroundWorker。您可以指定在后台线程上调用什么函数,以及在后台工作完成时调用另一个函数。

使用a。在使用WPF设计的GUI中使用BackgroundWorker安全吗?@q0987:
BackgroundWorker
将在WinForms或WPF中工作。它所做的只是为您管理线程,并且独立于所使用的技术。