From 6e7d363e82b03f7b99271d8e4d516f2d8e427ca3 Mon Sep 17 00:00:00 2001 From: zs <zhousong@weben-smart.com> Date: 周四, 21 11月 2024 09:29:33 +0800 Subject: [PATCH] 关闭结束所有进程 --- DataCapture_MA/Form1.cs | 26 ++++++++++++++++++++------ 1 files changed, 20 insertions(+), 6 deletions(-) diff --git a/DataCapture_MA/Form1.cs b/DataCapture_MA/Form1.cs index 74496c1..d568930 100644 --- a/DataCapture_MA/Form1.cs +++ b/DataCapture_MA/Form1.cs @@ -22,6 +22,7 @@ { public partial class CaptrueForm : Form { + private CancellationTokenSource _cts = new CancellationTokenSource(); bool RunningFlag = false; Socket skt = WZ.Useful.Commons.NetworkUtil.CreateUdpSocket(); EndPoint RemotePoint = null; @@ -69,16 +70,29 @@ string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); // 鍚姩鏂囦欢澶硅矾寰� CreateShortcut(shortcutName, targetPath, startupPath); // 鍒涘缓蹇嵎鏂瑰紡 - if (ipSelect.Items.Count > 0) - { - StartReceive(); // 鍚姩绋嬪簭 - CreateDatabase(); // 鍒濆鍖栨暟鎹簱 - } + StartReceive(); // 鍚姩绋嬪簭 + CreateDatabase(); // 鍒濆鍖栨暟鎹簱 // 鍒犻櫎涓�涓湀鍓嶇殑鏁版嵁 new Thread(AutoDeleteHistoryDate.Handler).Start(); + + this.FormClosing += CaptrueForm_FormClosing; + this.FormClosed += CaptrueForm_FormClosed; + } + private void CaptrueForm_FormClosing(object sender, FormClosingEventArgs e) + { + // 閫氱煡鍚庡彴浠诲姟鍙栨秷 + _cts.Cancel(); + + // 纭繚浠诲姟瀹屾垚 + Task.WaitAll(Task.WhenAll()); } + private void CaptrueForm_FormClosed(object sender, FormClosedEventArgs e) + { + // 纭繚绋嬪簭閫�鍑� + Environment.Exit(0); + } public static void CreateShortcut(string shortcutName, string targetPath, string startupPath) { @@ -92,7 +106,7 @@ shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); shortcut.Save(); } - + private void ReceiveHandle() { //鎺ユ敹鏁版嵁澶勭悊绾跨▼ -- Gitblit v1.9.3