Awaiting Asynchronous function inside FormClosing Event

后端 未结 6 1148
星月不相逢
星月不相逢 2020-11-27 20:59

I\'m having a problem where I cannot await an asynchronous function inside of the FormClosing event which will determine whether the form close should continue. I have crea

6条回答
  •  被撕碎了的回忆
    2020-11-27 21:34

    I needed to abort closing the form if an exeption was raised during the execution of an async method.

    I'm actually using a Task.Run with .Wait()

    private void Example_FormClosing(object sender, FormClosingEventArgs e)
    {
        try
        {
            Task.Run(async () => await CreateAsync(listDomains)).Wait();
        }
        catch (Exception ex)
        {
            MessageBox.Show($"{ex.Message}", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Error);
            e.Cancel = true;
        }
    }
    

提交回复
热议问题