Updating UI from events using asyc await

前端 未结 4 1484
北海茫月
北海茫月 2020-12-31 00:16

I am trying to understand how to update a UI from an event while using async/await pattern. Below is the test code I am using on a WinForm app. I am not even sure this is th

4条回答
  •  天命终不由人
    2020-12-31 00:49

    Here is another example

    async void DoExport()
    {
        var rMsg = "";
        var t = await Task.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);
              }));
        }
    }
    

提交回复
热议问题