1.基本用法
1 //Task.Run(() => this.DoSomethingLong("btnTask_Click1")); 2 //Task.Run(() => this.DoSomethingLong("btnTask_Click2")); 3 //TaskFactory taskFactory = Task.Factory;//4.0 4 //taskFactory.StartNew(() => this.DoSomethingLong("btnTask_Click3")); 5 //new Task(() => this.DoSomethingLong("btnTask_Click4")).Start();
2.线程等待,task.WaitAll
1 ////什么时候用多线程? 任务能并发运行;提升速度;优化体验 2 List<Task> taskList = new List<Task>(); 3 Console.WriteLine($"项目经理启动一个项目。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 4 Console.WriteLine($"前置的准备工作。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 5 Console.WriteLine($"开始编程。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 6 taskList.Add(Task.Run(() => this.Coding("爱书客", "Client")));//task--hash-- 7 taskList.Add(Task.Run(() => this.Coding("风动寂野", "Portal"))); 8 taskList.Add(Task.Run(() => this.Coding("笑看风云", "Service"))); 9 //做个子类 子类里面包含了一个属性 绿叶种子写个例子 10 taskList.Add(Task.Run(() => this.Coding("Jack", "Jump"))); 11 taskList.Add(Task.Run(() => this.Coding("胡萝卜", "Monitor"))); 12 Task.WaitAll(taskList.ToArray());
3.等限定的时间,然后继续
1 ////Task.WaitAll(taskList.ToArray(), 1000);//限时等待
4.等待某一个
1 //////阻塞:需要完成后再继续 2 //Task.WaitAny(taskList.ToArray());//会阻塞当前线程,等着某个任务完成后,才进入下一行 卡界面 3 ////Task.WaitAny(taskList.ToArray(), 1000); 4 //Console.WriteLine($"完成里程碑 【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
5.不卡主线程等待 WhenAll WhenAny
1 //taskList.Add(Task.WhenAny(taskList.ToArray()).ContinueWith(t => 2 //{ 3 // Console.WriteLine(taskList.ToArray().FirstOrDefault(s => s.Status == TaskStatus.RanToCompletion)); 4 // Console.WriteLine($"得意的笑。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 5 //})); 6 7 //taskList.Add(Task.WhenAll(taskList.ToArray()).ContinueWith(t => 8 //{ 9 // Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 10 //}));
6.上述taskFactory 写法
1 ////TaskFactory taskFactory = new TaskFactory(); 2 ////taskFactory.ContinueWhenAll(taskList.ToArray(), tList => 3 ////{ 4 //// Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 5 ////}); 6 7 ////taskFactory.ContinueWhenAny(taskList.ToArray(), t => 8 ////{ 9 //// Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 10 ////});
7.查看是哪个线程先完成任务的,taskFactory.StartNew的重载方法,可以拿到自定义的标识
1 //TaskFactory taskFactory = new TaskFactory(); 2 //List<Task> taskList = new List<Task>(); 3 //taskList.Add(taskFactory.StartNew(o => this.Coding("爱书客", "Client"), "爱书客")); 4 //taskList.Add(taskFactory.StartNew(o => this.Coding("风动寂野", "Portal"), "风动寂野")); 5 //taskList.Add(taskFactory.StartNew(o => this.Coding("笑看风云", "Service"), "笑看风云")); 6 //taskFactory.ContinueWhenAny(taskList.ToArray(), t => 7 //{ 8 //Console.WriteLine(t.AsyncState); 9 //Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 10 //}); 11 //taskFactory.ContinueWhenAll(taskList.ToArray(), tList => 12 //{ 13 //Console.WriteLine(tList[0].AsyncState); 14 //Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】"); 15 //});
8.回调
1 Task.Run(() => this.Coding("爱书客", "Client")).ContinueWith(t => { });
9.延迟和等待,卡界面 和不卡界面
1 //{ 2 // //Task.Delay(1000);//延迟 不会卡 3 // //Thread.Sleep(1000);//等待 卡 4 5 // Stopwatch stopwatch = new Stopwatch(); 6 // stopwatch.Start(); 7 // Thread.Sleep(2000); 8 // stopwatch.Stop(); 9 // Console.WriteLine(stopwatch.ElapsedMilliseconds); 10 //} 11 { 12 //Stopwatch stopwatch = new Stopwatch(); 13 //stopwatch.Start(); 14 //Task.Delay(2000).ContinueWith(t => 15 //{ 16 // stopwatch.Stop(); 17 // Console.WriteLine(stopwatch.ElapsedMilliseconds); 18 //}); 19 }
10.最多启动11条线程去完成任务(控制线程最大数量)
//{ // List<int> list = new List<int>(); // for (int i = 0; i < 10000; i++) // { // list.Add(i); // } // //完成10000个任务 但是只要11个线程 // Action<int> action = i => // { // Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString("00")); // Thread.Sleep(new Random(i).Next(100, 300)); // }; // List<Task> taskList = new List<Task>(); // foreach (var i in list) // { // int k = i; // taskList.Add(Task.Run(() => action.Invoke(k))); // if (taskList.Count > 10) // { // Task.WaitAny(taskList.ToArray()); // taskList = taskList.Where(t => t.Status != TaskStatus.RanToCompletion).ToList(); // } // } // Task.WhenAll(taskList.ToArray()); //}