简体   繁体   English

showdialog表单问题上的任务异步数据表

[英]task async datatable on showdialog form problem

I want to create the simple async popup windows application, and the windows form is showdialog at end.我想创建简单的异步弹出 windows 应用程序,windows 表单最后是 showdialog。 But when I run the program, the program auto exit without error Can you help me solve, here is my code但是当我运行程序时,程序自动退出没有错误你能帮我解决吗,这是我的代码

    private static string pridbUser = "user";
    private static string pridbPwd = "abc123";
    public void run()
    {
        string cnnStr = @"Data Source=localhost;Initial Catalog=sampledb;User ID=" + pridbUser + ";Password=" + pridbPwd + ";MultipleActiveResultSets=true";
        string sql = "SELECT * FROM [TABLE1]";
        using (SqlConnection sqlcnn = new SqlConnection(cnnStr))
        {
            sqlcnn.Open();
            var tskJob = CreateTableAsync(sql, sqlcnn);                
            using (frmUI ui = new frmUI())
            {
                Task.WhenAll(tskJob);
                ui.BindControl(tskJob.Result.DefaultView);
                ui.ShowDialog();
            }

        }        
    }
    private async Task<DataTable> CreateTableAsync(string sql, SqlConnection dbCnn)
    {

        using (SqlCommand _c = new SqlCommand(sql, dbCnn))
        {
            DataTable dt = new DataTable();
            dt.Load(await _c.ExecuteReaderAsync());
            return dt;
        }

    }

Revise, hi, thanks for your comment, then I rewrite the async job, but the result is not my want, the code below:修改一下,嗨,谢谢你的评论,然后我重写了异步作业,但是结果不是我想要的,代码如下:

private static string pridbUser = "user";
private static string pridbPwd = "abc123";
public void run()
{
    string cnnStr = @"Data Source=localhost;Initial Catalog=sampledb;User ID=" + pridbUser + ";Password=" + pridbPwd + ";MultipleActiveResultSets=true";
    string sql = "SELECT * FROM [TABLE1]";
    using (SqlConnection sqlcnn = new SqlConnection(cnnStr))
    {
        sqlcnn.Open();
        Console.WriteLine("Create Table with Asyn mode");
        var tskJob = CreateTableAsync(sql, sqlcnn);                
        using (frmUI ui = new frmUI())
        {
            Console.WriteLine("Create Form Complete");
            Task.WhenAll(tskJob);
            ui.BindControl(tskJob.Result.DefaultView);
            ui.ShowDialog();
        }

    }        
}
private async Task<DataTable> CreateTableAsync(string sql, SqlConnection dbCnn)
{
    Console.WriteLine("Start to Create Table")
    using (SqlCommand _c = new SqlCommand(sql, dbCnn))
    {
        DataTable dt = new DataTable();
        dt.Load(await _c.ExecuteReaderAsync());
        Console.WriteLine("Delay 5 second")
        Thread.Sleep(5000); // delay 5 seconds
        Console.WriteLine("End Delay")
        return dt;
    }

}

The running result is:运行结果是:

Create Table with Asyn mode
Start to Create Table
Delay 5 second
End Delay and Return
Create the Form Complete
Bind Control

But my expect result should be但我的预期结果应该是

Create Table with Asyn mode
Start to Create Table
Create the Form Complete
Delay 5 second <-- as it should not wait 5 second and then run form in async process
End Delay and Return
Bind Control

Can you have any good advise你有什么好的建议吗

You need to await the call to Task.WhenAll() (or call Wait() , if you can't, as shown in the Examples section of Task.WhenAll Method doco .您需要等待对Task.WhenAll()的调用(或调用Wait() ,如果不能,如Task.WhenAll Method doco的示例部分所示。

Also in your CreateTableAsync you should await the Task.Delay call.同样在您的CreateTableAsync中,您应该等待 Task.Delay 调用。

Here are some suggestions:以下是一些建议:

var tskJob = CreateTableAsync(sql, sqlcnn);   
tskJob.Wait() // Normally, you should use await if possible             
            using (frmUI ui = new frmUI())
            {
                // not needed Task.WhenAll(tskJob);
                ui.BindControl(

(...)

// Change Thread.Sleep(5000); // delay 5 seconds
//to
await Task.Delay(TimeSpan.FromSeconds(5));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM