How to wait for thread to complete without blocking UI

后端 未结 2 903
-上瘾入骨i
-上瘾入骨i 2020-12-05 03:44

I want my program to wait after below line

frmProgressBarObj = PullMSI.ExtractByMSIName(\"products.txt\", false);

as above method is intern

2条回答
  •  萌比男神i
    2020-12-05 04:01

    If you're using .NET 4.0 (with VS2012) or above, you can do this quite easily with the Task Parallel Library and async-await:

    private async void button1_Click(object sender, EventArgs e)
    {
        frmProgressBar frmProgressBarObj = await Task.Run(() =>
                          PullMSI.ExtractByMSIName("products.txt", false));
    
        MessageBox.Show(string.Format("Returned {0}", frmProgressBarObj.ToString());
    }
    

    For .NET 4, you'll need to add Microsoft.Bcl.Async.

提交回复
热议问题