异步显示对话框

本文关键字:对话框 显示 异步 | 更新日期: 2023-09-27 18:19:40

我使用async/await从数据库异步加载数据,在加载过程中,我想弹出一个加载表单,这只是一个简单的表单,带有运行进度条,指示有一个正在运行的进程。加载数据后,对话框将自动关闭。我怎样才能做到这一点?以下是我当前的代码:

 protected async void LoadData() 
    {
       ProgressForm _progress = new ProgressForm();  
       _progress.ShowDialog()  // not working
       var data = await GetData();          
       _progress.Close();
    }

更新:

我设法通过更改代码使其工作:

 protected async void LoadData() 
        {
           ProgressForm _progress = new ProgressForm();  
           _progress.BeginInvoke(new System.Action(()=>_progress.ShowDialog()));
           var data = await GetData();          
           _progress.Close();
        }

这是正确的方法还是有更好的方法?

谢谢你的帮助。

异步显示对话框

使用Task.Yield很容易实现,如下所示(WinForms,为了简单起见,没有异常处理)。重要的是要理解执行流如何跳到此处的新嵌套消息循环(模式对话框的消息循环),然后返回到原始消息循环(这就是await progressFormTask的作用):

namespace WinFormsApp
{
  internal static class DialogExt
  {
    public static async Task<DialogResult> ShowDialogAsync(this Form @this)
    {
      await Task.Yield();
      if (@this.IsDisposed)
        return DialogResult.Cancel;
      return @this.ShowDialog();
    }
  }
  public partial class MainForm : Form
  {
    public MainForm()
    {
      InitializeComponent();
    }
    async Task<int> LoadDataAsync()
    {
      await Task.Delay(2000);
      return 42;
    }
    private async void button1_Click(object sender, EventArgs e)
    {
      var progressForm = new Form() { 
        Width = 300, Height = 100, Text = "Please wait... " };
      object data;
      var progressFormTask = progressForm.ShowDialogAsync();
      try 
      {
        data = await LoadDataAsync();
      }
      finally 
      {
        progressForm.Close();
        await progressFormTask;
      }
      // we got the data and the progress dialog is closed here
      MessageBox.Show(data.ToString());
    }
  }
}

下面是一个使用Task.ContinueWith的模式,使用模态ProgressForm:应该可以避免任何竞争条件

protected async void LoadDataAsync()
{
    var progressForm = new ProgressForm();
    // 'await' long-running method by wrapping inside Task.Run
    await Task.Run(new Action(() =>
    {
        // Display dialog modally
        // Use BeginInvoke here to avoid blocking
        //   and illegal cross threading exception
        this.BeginInvoke(new Action(() =>
        {   
            progressForm.ShowDialog();
        }));
        
        // Begin long-running method here
        LoadData();
    })).ContinueWith(new Action<Task>(task => 
    {
        // Close modal dialog
        // No need to use BeginInvoke here
        //   because ContinueWith was called with TaskScheduler.FromCurrentSynchronizationContext()
        progressForm.Close();
    }), TaskScheduler.FromCurrentSynchronizationContext());
}

ShowDialog()是一个阻塞调用;在用户关闭对话框之前,执行不会前进到await语句。请改用Show()。不幸的是,您的对话框将不是模态的,但它将正确地跟踪异步操作的进度。

总是调用ShowDialog()LoadDataAsyncClose(),以避免像@noseration的答案中那样调用IsDisposed。所以使用Task.Yield()来延迟LoadDataAsync()而不是ShowDialog()

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    async Task<int> LoadDataAsync()
    {
        Console.WriteLine("Load");
        await Task.Delay(2000);
        return 42;
    }
    private async void button1_Click(object sender, EventArgs e)
    {
        var progressForm = new Form()
        {
            Width = 300,
            Height = 100,
            Text = "Please wait... "
        };
        async Task<int> work()
        {
            try
            {
                await Task.Yield();
                return await LoadDataAsync();
            }
            finally
            {
                Console.WriteLine("Close");
                progressForm.Close();
            }
        }
        var task = work();
        Console.WriteLine("ShowDialog");
        progressForm.ShowDialog();
        object data = await task;
        // we got the data and the progress dialog is closed here
        Console.WriteLine("MessageBox");
        MessageBox.Show(data.ToString());
    }
}

您可以尝试以下操作:

protected async void LoadData()
{
    ProgressForm _progress = new ProgressForm();
    var loadDataTask = GetData();
    loadDataTask.ContinueWith(a =>
        this.Invoke((MethodInvoker)delegate
        {
            _progress.Close();
        }));
    _progress.ShowDialog();
}