static async Task RunProgram() { UitHelper.PrintF(PrintEnum.program, "RunProgram"); var taskQueue = new ConcurrentQueue <CustomerTask>(); var cts = new CancellationTokenSource(); var taskSource = Task.Run(() => TaskProducer(taskQueue)); UitHelper.PrintF(PrintEnum.program, "RunProgram"); Task[] processors = new Task[4]; for (int i = 1; i <= 4; i++) { string processid = i.ToString(); processors[i - 1] = Task.Run(() => TaskProcessor(taskQueue, "processor " + processid, cts.Token)); } UitHelper.PrintF(PrintEnum.program, "RunProgram"); await taskSource; UitHelper.PrintF(PrintEnum.program, "RunProgram"); cts.CancelAfter(TimeSpan.FromSeconds(2)); await Task.WhenAll(processors); UitHelper.PrintF(PrintEnum.program, "RunProgram"); }
public static void Print() { var name = "Main"; Task t = RunProgram(); UitHelper.PrintF(PrintEnum.main, "Print"); t.Wait(); UitHelper.PrintF(PrintEnum.main, "Print"); }