发表更新1 分钟读完 (大约140个字)
【Csharp】Thread匿名方法初识
片段代码笔记
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
private void StopRuningTaskOrThreadAndExitOrRestart(bool restartFlag) { timer1.Enabled = false; if (switchstr == "1" && threadswitchstr == "1") { Thread t = new Thread((flag) => { while (threadUseNum > 0) { Thread.Sleep(5000); RefreshMsgBox(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"\t=>\t等待子任务执行完成退出:子任务数量:{threadUseNum}"); } if ((bool)flag) { Process.Start(Application.ExecutablePath); } Environment.Exit(0); }); t.IsBackground = true; t.Start(restartFlag); } else if (switchstr == "1") { Thread t = new Thread((flag) => { if (taskThread != null) { taskThread.Wait(); } if ((bool)flag) { Process.Start(Application.ExecutablePath); } Environment.Exit(0); }); t.IsBackground = true; t.Start(restartFlag); } }
|