【问题标题】:Using Async await in Windows Forms Load event to load other data controls?在 Windows 窗体加载事件中使用异步等待来加载其他数据控件?
【发布时间】:2016-05-23 00:32:55
【问题描述】:

我们有一个打开 ChildForm 的 Windows 窗体 ParentForm。类似于以下内容:

var form = new ChildForm(parm1, parm2, parm3, parm4);
form.StartPosition = FormStartPosition.CenterParent;
form.ShowDialog();

ChildForm 的构造函数如下所示:

public ChildForm(string Parm1, string Parm2, string Parm3, string Parm4)
{
    InitializeComponent();

    FillThisComboBox();
    FillThisOtherComboBox();
    BindAnotherCombobox();
    BindGridview();
    FillCheckBoxList();
    FillAnotherCheckBoxList();
}

不幸的是,ChildForm 需要一段时间才能加载,因为它会在实际绘制表单之前运行所有这些方法。

我想使用async await 执行所有这些方法,以便在所有 ekse 运行时绘制表单,并且我尝试了类似以下的操作,但我继续收到构建错误:

private async void ChildForm_Load(object sender, EventArgs e)
{
    await PrepareControls();
}
private async Task PrepareControls()
{
    FillThisComboBox();
    FillThisOtherComboBox();
    BindAnotherCombobox();
    BindGridview();
    FillCheckBoxList();
    FillAnotherCheckBoxList();
}

感谢任何帮助。谢谢。

【问题讨论】:

  • 数据从何而来?

标签: c# winforms asynchronous visual-studio-2015


【解决方案1】:

Async/Await 的一些规则如下:

  1. 任何标记为异步的方法都必须返回 void、task 或某事的任务。
  2. 异步事件处理程序返回 void 以匹配 EventHanlder 定义/签名
  3. 使其他方法异步通常遵循以下模式:

    public async Task<ofSomething> DoSomething(){
         //entry here is on the caller's thread.
         var x =await Task<ofSomething>.Run(()=>{
             //this code will be run independent of calling thread here
             return GetSomething();
          }
         //at this point you are back on the caller's thread.
         //the task.result is returned.
         return x;
    } 
    

每个异步方法都有一个或多个“等待”一个值的语句。变量 x 是“关闭返回 OfSomeThing 类型的 GetSomething 结果的任务。

要在返回 void 的 eventthanlder 中使用上面的代码...正如您已经知道的,您必须输入 async 关键字

public async void ClickEventHandler(object sender, EvantArgs e){
    var x = await DoSomething();
    //var x now has the ofSomething object on the gui thread here.
}

请注意,单击处理程序返回 void 以满足单击处理程序的委托签名,但它调用了不返回 void 的异步方法。

并发、错误处理和取消都很重要,所以也要研究这些。欢迎来到异步的世界。异步世界让事情变得更加容易。

警告总是这样做

2012 年,Stephen Cleary 写了一篇关于 Configure.Await(false) 的文章。

我随后在一个真实的应用程序中学会了确保始终这样做。它大大加快了速度并避免了其他问题。

【讨论】:

  • 异步让世界变得更加复杂,但async - await 技术让异步世界变得更容易:)
  • Async/Await 让它看起来简单,但是等到你在 UI 代码中遇到第一个死锁,然后你哭着跑到the Jon Skeet of Async 然后你花了三天时间阅读并意识到:不,这并不容易,只是更容易打字。
  • 购买 Stephen Cleary 的书。
【解决方案2】:

查看我的两个表单项目。您可以在构造函数之后调用 PrepareControls,并使用表单的实例加载函数。您可以使用 form.Show() 或 form.ShowDialog() 取决于您是否要等待子窗体关闭或不关闭。

表格 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form2.Show();
            string  results = form2.GetData();
        }
    }
}

表格 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 nform1)
        {
            InitializeComponent();

            this.FormClosing +=  new FormClosingEventHandler(Form2_FormClosing);
            form1 = nform1;
            form1.Hide();
        }
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //stops form from closing
            e.Cancel = true;
            this.Hide();
        }
        public string GetData()
        {
            return "The quick brown fox jumped over the lazy dog";
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 2022-01-19
    • 2016-11-20
    • 2012-08-06
    • 1970-01-01
    • 2010-09-28
    • 2013-02-26
    • 2018-01-07
    相关资源
    最近更新 更多