C# 在Windows窗体加载事件中使用异步等待加载其他数据控件?

C# 在Windows窗体加载事件中使用异步等待加载其他数据控件?,c#,winforms,asynchronous,visual-studio-2015,C#,Winforms,Asynchronous,Visual Studio 2015,我们有一个Windows窗体ParentForm,它打开ChildForm。如下所示: var form = new ChildForm(parm1, parm2, parm3, parm4); form.StartPosition = FormStartPosition.CenterParent; form.ShowDialog(); ChildForm的构造函数如下所示: public ChildForm(string Parm1, string Parm2, string Parm3,

我们有一个Windows窗体
ParentForm
,它打开
ChildForm
。如下所示:

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

感谢您的帮助。谢谢。

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

表格一

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();
        }
    }
}
表格二

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

    }
}

请参阅我的双表单项目。您可以使用表单的实例在构造函数和加载函数之后调用PrepareControls。您可以使用form.Show()或form.ShowDialog(),具体取决于是否要等待子窗体关闭

表格一

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();
        }
    }
}
表格二

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

    }
}

异步/等待的一些规则如下:

  • 任何标记为async的方法都必须返回void、task或某个对象的task
  • 异步事件处理程序返回void以匹配EventHandler定义/签名
  • 使其他方法异步通常遵循以下模式:

    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;
    } 
    
    请注意,click处理程序返回void以满足clickhandler的委托签名,但它调用一个不返回void的异步方法

    并发、错误处理和取消都很重要,所以也要研究它们。欢迎来到异步世界。异步世界让事情变得简单多了

    警告始终执行此操作

    2012年,斯蒂芬·克利里(Stephen Cleary)写了一篇关于


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

    异步/等待的一些规则如下:

  • 任何标记为async的方法都必须返回void、task或某个对象的task
  • 异步事件处理程序返回void以匹配EventHandler定义/签名
  • 使其他方法异步通常遵循以下模式:

    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;
    } 
    
    请注意,click处理程序返回void以满足clickhandler的委托签名,但它调用一个不返回void的异步方法

    并发、错误处理和取消都很重要,所以也要研究它们。欢迎来到异步世界。异步世界让事情变得简单多了

    警告始终执行此操作

    2012年,斯蒂芬·克利里(Stephen Cleary)写了一篇关于


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

    数据从哪里来?数据从哪里来?异步使世界变得更加复杂,但
    Async-await
    技术使异步世界变得更容易:)Async/await使它看起来简单,但要等到您的UI代码中出现第一个死锁,你哭着跑,然后花了三天时间阅读并意识到:不,这并不是很容易,只是更容易打字。买斯蒂芬·克利里的书。异步使世界变得复杂,但
    Async-await
    技术使异步世界变得更容易:)Async/await使它看起来简单,但是等到你的UI代码遇到第一个死锁,你哭着跑,然后你花了三天的时间阅读并意识到:不,这并不是很容易,只是更容易键入。买斯蒂芬·克利里的书。