上边的主线程会被卡住:
7个同时开发,谁先完成之后, 需要完成某个工作,可以用waitany
2: 不卡界面的做法
加上以上命令就不卡,是子线程做的事
//Task task = taskFactory.StartNew(action); Console.WriteLine("Eleven老师接到一个项目"); Console.WriteLine("沟通需求,谈妥价格"); Console.WriteLine("签合同,收取50%的费用"); Console.WriteLine("高级班筛选学员,组建团队"); Console.WriteLine("详细的需求分析、系统设计、模块划分、框架搭建"); Console.WriteLine("开始干活。。。"); Console.WriteLine(" "); List<Task> taskList = new List<Task>(); taskList.Add(taskFactory.StartNew(() => this.Coding("浑无痕", "Portal"))); taskList.Add(taskFactory.StartNew(() => this.Coding(" 菜鸟 ", "BackService"))); taskList.Add(taskFactory.StartNew(() => this.Coding("oldwork", "Client"))); taskList.Add(taskFactory.StartNew(() => this.Coding(" 米 ", "WeChatClient"))); taskList.Add(taskFactory.StartNew(() => this.Coding("走自己的路", " WCF "))); taskList.Add(taskFactory.StartNew(() => this.Coding("滑猪小板", " DB "))); taskList.Add(taskFactory.StartNew(() => this.Coding("Walden", "小程序"))); //都开发完成后,进行联调测试 不是我做的,是开发者 taskFactory.ContinueWhenAny(taskList.ToArray(), t => Console.WriteLine("第一个完成的,获取红包奖励,{0}", Thread.CurrentThread.ManagedThreadId)); taskFactory.ContinueWhenAll(taskList.ToArray(), tList => Console.WriteLine("都开发完成后,开始联调测试,{0}", Thread.CurrentThread.ManagedThreadId)); Task.WaitAny(taskList.ToArray());//等待任意一个任务的完成,会卡主当前的运行线程 Console.WriteLine("某个模块完成开发后,老师再收取20%的费用"); Task.WaitAll(taskList.ToArray());//等待全部任务的完成,会卡主当前的运行线程 Console.WriteLine("项目开发验收完成后,老师去收取全部的费用"); Console.WriteLine("老师给大家分钱。。。"); Console.WriteLine("******************btnTask_Click 异步方法 end {0}********************", Thread.CurrentThread.ManagedThreadId); Console.WriteLine(); } /// <summary> /// Coding /// </summary> /// <param name="name"></param> private void Coding(string name, string project) { Console.WriteLine("******************Coding start {0} {1} {2} {3}********************", name, project, Thread.CurrentThread.ManagedThreadId.ToString("00"), DateTime.Now.ToString("HHmmss:fff")); long lResult = 0; for (int i = 0; i < 100000000; i++) { lResult += i; } Thread.Sleep(2000); Console.WriteLine("******************Coding end {0} {1} {2} {3}********************", name, project, Thread.CurrentThread.ManagedThreadId.ToString("00"), DateTime.Now.ToString("HHmmss:fff")); }
技术交流qq群:143280841