【问题标题】:Updating UI from events using asyc await使用异步等待从事件更新 UI
【发布时间】:2020-06-13 03:11:56
【问题描述】:

我正在尝试了解如何在使用异步/等待模式时从事件更新 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);
        }
   }

-

【问题讨论】:

    标签: c# asynchronous async-await


    【解决方案1】:

    The async pattern has support for progress updates.

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

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

    【讨论】:

    • // calling code 部分中的ProcessWithProgress 是否应该改为ProcessWithUpdates
    【解决方案2】:

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

    简单示例:

    void SomeAsyncMethod()
    {
        // Do some work             
    
        if (this.InvokeRequired)
        {
            this.Invoke((MethodInvoker)(() =>
                {
                    DoUpdateUI();
    
                }
            ));
        }
        else
        {
            DoUpdateUI();
        }
    }
    
    void DoUpdateUI()
    {
        // Your UI update code here
    }
    

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

    【讨论】:

    • 这和进度建议一样好用。我正在使用这两种方法,非常感谢。
    • 你不应该在使用异步模式时自己实现这个。 Progress&lt;T&gt; 已在创建它的 SynchronizationContext 上执行。
    • 简单的解决方案。效果很好。
    【解决方案3】:

    //像这样声明一个委托

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

    【讨论】:

      【解决方案4】:

      这是另一个例子

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-22
        • 2017-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-02-24
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        相关资源
        最近更新 更多