C#上位机序列3: 流程控制(串行,并行,混合)
封装Task库实现几种生产流程控制
示例1:串行执行
14:55:34.081 A
14:55:35.085 B
14:55:36.094 C
示例2:A执行完 BC并行,且C执行完继续执行
14:55:58.843 A
14:55:58.875 B
14:55:58.875 C1
14:55:58.875 C2: 100
示例3:A执行完 BC并行
14:56:14.682 A
14:56:15.689 C
14:56:15.692 B
示例4:BC并行全部执行完 A
14:56:33.054 B
14:56:34.062 C
14:56:35.078 A
示例5:BC并行任意执行完 A
14:56:58.431 C成功
14:56:59.445 C
14:56:59.445 A
14:57:00.453 B
示例6:执行超时
14:57:26.555 很可惜,没有完成!
using System; using System.Threading; using System.Threading.Tasks; using TaskManage.util; namespace TaskManage { public class Test { static CancellationTokenSource cts = new CancellationTokenSource(); static ManualResetEvent resetEvent = new ManualResetEvent(true); public static async Task Start() { //Task.Run(() => //{ //}); //Worker1();// 串行 //Worker2();// 并行,带继续执行 //Worker3();// A执行完 BC并行 //Worker4();// BC并行全部执行完 A //Worker5();// BC并行任意执行完 A Worker6();// 执行超时 } static void Log(string msg) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} {msg}"); } static async Task Worker(string name, int time = 1000) { await Task.Delay(time); if (cts.Token.IsCancellationRequested) return; resetEvent.WaitOne(); Log(name); } // A、B、C依次串行 // A->B->C static async Task Worker1() { await Worker("A"); await Worker("B"); await Worker("C"); } // A、B、C全部并行 // A // B // C // Completed static async Task Worker2() { //Task firstTask = Task.Run(() => { Console.WriteLine("Step1"); }); //Action<object> parameterizedMethod = (value) => { Console.WriteLine($"Step2: {value}"); }; //Task secondTask = firstTask.ContinueWith(prevTask => { parameterizedMethod(42); }); //Task.WaitAll(firstTask, secondTask); children children = new children(cts); children.go(() => Worker("A", 1000)); children.go(() => Worker("B", 1000)); Action<object> Completed = (value) => { Log($"C2: {value}"); }; children.go(() => Worker("C1", 1000), Completed, 100); } // A执行完后,B、C再并行 // -->B // | //A-> // | // -->C static async Task Worker3() { await Worker("A"); children children = new children(cts); children.go(() => Worker("B")); children.go(() => Worker("C")); //children.wait_all(); } // B、C都并行执行完后,再执行A //B-- // | // -->A // | //C-- static async Task Worker4() { //Func<Task> a = () => Worker("A", 1000); //Func<Task> b = () => Worker("B", 2000);//Func<Task> //Task t1 = Task.Run(a); //Task t2 = Task.Run(b); //List<Task> lt = new List<Task>(); //lt.Add(t1); //lt.Add(t2); //Task.WaitAll(lt.ToArray()); //Console.WriteLine("WaitAll Done"); children children = new children(cts); children.go(() => Worker("B", 1000)); children.go(() => Worker("C", 2000)); children.wait_all(); await Worker("A"); } // B、C任意一个执行完后,再执行A //B-- // | // >-->A // | //C-- static async Task Worker5() { // 不需要知道谁第1 //children children = new children(cts); //children.go(() => Worker("B", 2000)); //children.go(() => Worker("C", 1000)); //children.wait_any(); //await Worker("A"); // 需要知道谁第1 children children = new children(cts); Task B = Task.Run(() => Worker("B", 2000), cts.Token); Task C = Task.Run(() => Worker("C", 1000), cts.Token); var task = children.when_any(B, C); if (task == B) { Log("B成功"); } else { Log("C成功"); } await Worker("A"); } static void Worker6() { children children = new children(cts); children.go(() => Worker("B", 2000)); var result = children.when_any(1000).Result; if (!result) { Log("很可惜,没有完成!"); } } } }
qq:505645074