【发布时间】:2020-06-22 22:37:24
【问题描述】:
我想通过单击其他按钮来了解 A 按钮事件中运行的任务状态。 像这样。
private void button1_Click(object sender, EventArgs e)
{
Task.Run(()=>{
//The method to take long time
//For example
Thread.Sleep(5000)
;});
}
private coid button2_Click(object sender, EventArgs e)
{
//until 5000ms
//the method to know the above task status (Runnning....)
//after 5000ms
//the method to know the above task status (Conpleted....)
}
【问题讨论】:
-
获取
Task的状态并不是真正的挑战。从 Fayilt 的回答中可以看出,您所需要的只是对Task对象的引用。您想如何处理在执行Task期间多次单击Button1的用例。将创建多个Task对象,并且引用将指向最后一个。
标签: c# multithreading task