https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/await
private async void button1_Click(object sender, EventArgs e)
{
var progressReporter = new Progress<int>(ReportProgress);
await TimeConsumeFunction(progressReporter);
}
//更新界面的函数
private void ReportProgress(int val)
{
textBox1.Text = val.ToString() + "%";
}
//耗时操作函数
private Task TimeConsumeFunction(IProgress<int> progressRptr)
{
var task = Task.Run(() =>
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(10);//sleep演示耗时
progressRptr.Report(i);//报告进度
}
});
return task;
}