22

我试图了解如何在使用异步/等待模式时从事件更新 UI。下面是我在 WinForm 应用程序上使用的测试代码。我什至不确定这是正确的方法。允许 pwe_StatusUpdate 方法更新 UI 需要什么?那里抛出了跨线程操作错误。

谢谢阅读。

 // calling code
    ProcessWithEvents pwe = new ProcessWithEvents();
    pwe.StatusUpdate += pwe_StatusUpdate;
    await pwe.Run();



 void pwe_StatusUpdate(string updateMsg)
    {
      // Error Here: Cross-thread operation not valid: Control '_listBox_Output' accessed from a thread other than the thread it was created on.
      _listBox_Output.Items.Add(updateMsg);
    }

-

// Class with long running process and event    
public delegate void StatusUpdateHandler(string updateMsg);

 public class ProcessWithEvents
  {
    public event StatusUpdateHandler StatusUpdate;

    public async Task Run()
    {
        await Task.Run(() =>
        {
            for (int i = 0; i < 10; i++)
                {

                    RaiseUpdateEvent(String.Format("Update {0}", i));

                    Thread.Sleep(500);
                }
            });

        }

        private void RaiseUpdateEvent(string msg)
        {
        if (StatusUpdate != null)
            StatusUpdate(msg);
        }
   }

-

4

4 回答 4

29

async模式支持进度更新

简而言之,您的async方法可以采用IProgress<T>,并且您的调用代码会传入该接口的实现(通常是Progress<T>)。

public class ProcessWithUpdates
{
  public async Task Run(IProgress<string> progress)
  {
    await Task.Run(() =>
    {
      for (int i = 0; i < 10; i++)
      {
        if (progress != null)
          progress.Report(String.Format("Update {0}", i));
        Thread.Sleep(500);
      }
    });
  }
}

// calling code
ProcessWithUpdates pwp = new ProcessWithUpdates();
await pwp.Run(new Progress<string>(pwp_StatusUpdate));
于 2013-07-13T15:20:52.923 回答
4

您应该Invoke使用Control. 它在 Control 的线程中执行一些代码。您还可以检查InvokeRequired属性以检查是否需要调用Invoke方法(它检查调用者是否在与创建控件的线程不同的线程上)。

简单的例子:

void SomeAsyncMethod()
{
    // Do some work             

    if (this.InvokeRequired)
    {
        this.Invoke((MethodInvoker)(() =>
            {
                DoUpdateUI();

            }
        ));
    }
    else
    {
        DoUpdateUI();
    }
}

void DoUpdateUI()
{
    // Your UI update code here
}

在某些情况下,您应该在调用方法之前检查IsHandleCreated属性。如果返回 false 那么您需要等待 Control 的句柄将被创建ControlInvokeIsHandleCreated

于 2013-07-13T15:00:52.750 回答
1

// 像这样声明一个委托

delegate void Add(string msg);

//然后像这样声明委托方法:

var add = new Add((msg) => {
   _listBox_Output.Items.Add(msg);
});

//现在只需调用委托:

void pwe_StatusUpdate(string updateMsg)
    {

      _listBox_Output.Invoke(add,updateMsg);
    }
于 2013-07-13T15:13:43.257 回答
-3

这是另一个例子

async void DoExport()
{
    var rMsg = "";
    var t = await Task<bool>.Factory.StartNew(() => ExportAsMonthReport(LastMonth.Name, LastYear.Name, out rMsg));

    if (t)
    {
          BeginInvoke((Action)(() =>
          {
               spinnerMain.Visible = false;
               menuItemMonth.Enabled = true;

               MetroMessageBox.Show(this, rMsg, "Export", MessageBoxButtons.OK, MessageBoxIcon.Information, 200);
          }));
   }
   else
   {
          BeginInvoke((Action)(() =>
          {
               spinnerMain.Visible = false;
               menuItemMonth.Enabled = true;

               MetroMessageBox.Show(this, rMsg, "Export", MessageBoxButtons.OK, MessageBoxIcon.Error, 200);
          }));
    }
}
于 2017-08-21T01:43:24.213 回答