定时器加 Task 任务,执行异步方法。
private void timer1_test_Tick(object sender, EventArgs e)
{
ss(DateTime.Now.Second);
}
Task quanTask;
public async Task ss(int flag) {
if (quanTask!=null&&!quanTask.IsCompleted)
{
Console.WriteLine("-------上一次还未完成--------");
return;
}
Console.WriteLine(flag+"==============开始===========。。。。");
quanTask = Task.Run(() =>
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
});//委托方法
await quanTask;
if (quanTask.IsCompleted)
{
Console.WriteLine(flag + "==============完成===========。。。。");
}
}
标签:02,异步,Task,Console,quanTask,flag,WriteLine From: https://blog.51cto.com/51souta/5806018