Task状态

 1       Task task=null;
 2         private void button1_Click(object sender, EventArgs e)
 3         {
 4             task = Task.Factory.StartNew(() =>
 5               {
 6                   while (true)
 7                   {
 8                       //Thread.Sleep(10000);
 9                       //break;
10                       throw new Exception("1232");
11                   }
12               });
13         }
14         private void timer1_Tick(object sender, EventArgs e)
15         {
16             if (task == null)
17             {
18                 textBox1.Text = "还未实例化";
19             }
20             else
21             {
22                 textBox1.Text = task.Status.ToString();
23             }
24             //    public enum TaskStatus
25             //{
26             //    Created,
27             //    WaitingForActivation,
28             //    WaitingToRun,
29             //    Running,
30             //    WaitingForChildrenToComplete,
31             //    RanToCompletion,
32             //    Canceled,
33             //    Faulted
34             //}
35             //执行完成就是RanToCompletion
36             //有异常报错就是Faulted
37             //正在运行就是Running
38         }
39     //如果一个Task在运行,强制task=null了,那么这个线程还是在后台运行l

 

posted @ 2025-11-29 20:18  家煜宝宝  阅读(4)  评论(0)    收藏  举报