我读过的每一篇博客文章都告诉你如何在C#中使用异步方法,但出于某种奇怪的原因,从来没有解释过如何构建自己的异步方法来使用。所以我现在有这个代码来使用我的方法:
private async void button1_Click(object sender, EventArgs e)
{
var now = await CountToAsync(1000);
label1.Text = now.ToString();
}
我写的这个方法就是CountToAsync
private Task<DateTime> CountToAsync(int num = 1000)
{
return Task.Factory.StartNew(() =>
{
for (int i = 0; i < num; i++)
{
Console.WriteLine("#{0}", i);
}
}).ContinueWith(x => DateTime.Now);
}
这是使用Task.Factory
**,编写异步方法的最好方式,还是应该换一种方式来编写?**
https://stackoverflow.com/questions/16063520
复制相似问题