C#的计时器任务

 
 var task = Task.Run(() =>
            {
                System.Threading.Thread.Sleep(1000 * 40);
                Console.Write("111");
            });
            var taskSuccess = task.Wait(1000 * 30);
            if (!taskSuccess)
            {
                Console.WriteLine(task.IsCompleted);
                Console.Write("222");
            }

 结果:

False  222 

var task = Task.Run(() =>
            {
                System.Threading.Thread.Sleep(1000 * 20);
                Console.WriteLine("111");
            });
            var taskSuccess = task.Wait(1000 * 30);
            if (!taskSuccess)
            {
                Console.WriteLine(task.IsCompleted);
                Console.Write("222");
            }
            else
            {
                Console.WriteLine(task.IsCompleted);
                Console.WriteLine("333");
            }

结果:

111  true  333

 

posted @ 2019-12-24 09:38  土豆2号  阅读(216)  评论(0编辑  收藏  举报