//9 超时等待一组任务,然后中止所有任务,且在中止任务中就地善后处理 static async Task Worker9() { generator.children children = new generator.children(); children.go(() => Worker("A", 1000)); children.go(async delegate() { try { await Worker("B", 2000); } catch (generator.stop_exception) { Log("B被中止"); await generator.sleep(500); throw; } catch (System.Exception) { } }); var task = await children.timed_wait_all(1500); await children.stop(); Log($"成功{task.Count}个"); }
//8 超时等待一组任务,然后中止所有任务 static async Task Worker8() { generator.children children = new generator.children(); children.go(() => Worker("A", 1000)); children.go(() => Worker("B", 2000)); var tasks = await children.timed_wait_all(1500); await children.stop(); Log($"成功{tasks.Count}个"); }
//7 超时等待一个特定任务,然后中止所有任务 static async Task Worker7() { generator.children children = new generator.children(); var A = children.tgo(() => Worker("A", 1000)); var B = children.tgo(() => Worker("B", 2000)); if (await children.timed_wait(1500, A)) { Log("成功"); } else { Log("超时"); } await children.stop(); }
private async Task Task1Action(int time) { WaitForm waitAction = new WaitForm(); generator.children children = new generator.children(); generator.child taskChild = children.tgo(async delegate() { bool cancelTask = false; Task task = Task.Run(delegate() { int ct = time / 100; for (int i = 0; i < ct && !cancelTask; i++) { System.Threading.Thread.Sleep(100); } }); try { await generator.wait_task(task); } catch (generator.stop_exception) { cancelTask = true; await generator.wait_task(task); } }); children.go(async delegate() { await generator.sleep(500); await generator.send_control(this, delegate() { waitAction.StartPosition = FormStartPosition.CenterParent; waitAction.ShowDialog(); }); if (waitAction.isCancel) { taskChild.stop(); } }); await children.wait(taskChild); waitAction.Close(); await children.stop(); }
//11 嵌套中止 static async Task Worker11() { generator.children children = new generator.children(); children.go(() => Worker("A", 1000)); children.go(async delegate() { try { generator.children children1 = new generator.children(); children1.go(async delegate() { try { await Worker("B", 2000); } catch (generator.stop_exception) { Log("B被中止1"); await generator.sleep(500); throw; } catch (System.Exception) { } }); await children1.wait_all(); } catch (generator.stop_exception) { Log("B被中止2"); throw; } catch (System.Exception) { } }); await generator.sleep(1500); await children.stop(); }