From aefe9f2572eac7c61f6d2952593ec18a700dfcf0 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期二, 01 六月 2021 11:43:19 +0800
Subject: [PATCH] 1. 上传数据线程部分修改,界面关闭时退出所有相关线程

---
 CommonUtil.cs              |   37 ++++++++++--------
 MainForm.cs                |   26 ++++++++++--
 Properties/AssemblyInfo.cs |    4 +-
 Program.cs                 |    1 
 DetectionDataUpload.cs     |   43 +++++++++++++--------
 5 files changed, 72 insertions(+), 39 deletions(-)

diff --git a/CommonUtil.cs b/CommonUtil.cs
index 0e7b920..b9ed9bd 100644
--- a/CommonUtil.cs
+++ b/CommonUtil.cs
@@ -6,6 +6,7 @@
 using System.Runtime.InteropServices;
 using System.Windows;
 using System.Windows.Forms;
+using System.Threading;
 
 namespace M423project
 {
@@ -46,22 +47,22 @@
         private static object lockObj = new object();
         private static object logObj = new object();
 
-        private static bool uploadDataEnable = true;
+        //private static bool uploadDataEnable = true;
 
-        public static bool UploadDataEnable
-        {
-            get
-            {
-                lock (lockObj) { return uploadDataEnable; }
-            }
-            set
-            {
-                lock (lockObj)
-                {
-                    uploadDataEnable = value;
-                }
-            } 
-        }
+        //public static bool UploadDataEnable
+        //{
+        //    get
+        //    {
+        //        lock (lockObj) { return uploadDataEnable; }
+        //    }
+        //    set
+        //    {
+        //        lock (lockObj)
+        //        {
+        //            uploadDataEnable = value;
+        //        }
+        //    } 
+        //}
 
         private static DetectionOption detectionOption = DetectionOption.doProduct;
 
@@ -118,7 +119,11 @@
         /// 涓荤獥浣撳紩鐢�
         /// </summary>
         public static MainForm mainForm { get; set; }
- 
+
+        public static ManualResetEvent UploadDoneHandle = new ManualResetEvent(true);
+        public static object UploadDataLock = new object();
+        public static bool UploadDataEnable = true;
+
         static CommonUtil()
         {
             CurrentDir = Directory.GetCurrentDirectory();
diff --git a/DetectionDataUpload.cs b/DetectionDataUpload.cs
index 4d02940..536b414 100644
--- a/DetectionDataUpload.cs
+++ b/DetectionDataUpload.cs
@@ -23,7 +23,7 @@
     {
         #region 鏁版嵁涓婁紶鐩稿叧绉佹湁鎴愬憳
         private DetectionData detectionData;
-        private Thread uploadDataProcesser;
+        private Thread uploadDataProcesser = null;
         //private RunCard runCard;
         private ConfigStruct config;
         #endregion
@@ -43,33 +43,43 @@
         /// </summary>
         public void Start()
         {
-            uploadDataProcesser = new Thread(Execute);
-            uploadDataProcesser.Start();
+            if (uploadDataProcesser == null)
+            {
+                uploadDataProcesser = new Thread(Execute);
+                uploadDataProcesser.IsBackground = true;
+                uploadDataProcesser.Start();
+            }
         }
 
         public void Stop()
         {
-            if (uploadDataProcesser != null && uploadDataProcesser.IsAlive)
-            {
-                uploadDataProcesser.Abort();
-                uploadDataProcesser.Join();
-            }
-            uploadDataProcesser = null;
+            //if (uploadDataProcesser != null && uploadDataProcesser.IsAlive)
+            //{
+            //    uploadDataProcesser.Abort();
+            //    uploadDataProcesser.Join();
+            //}
+            //uploadDataProcesser = null;
         }
 
-        static object uploadLock = new object();
+        //static object uploadLock = new object();
         public void UploadData()
         {
-            lock (uploadLock)
+            lock (CommonUtil.UploadDataLock)
             {
+                if (!CommonUtil.UploadDataEnable)
+                    return;
+
                 DataTable dtUpload = detectionData.QueryUnloadData();
                 if (dtUpload.Rows.Count <= 0)
                     return;
+
                 if (dtUpload.Rows.Count < 20)
                 {
                     Thread.Sleep(2000);
                     dtUpload = detectionData.QueryUnloadData();
                 }
+
+                CommonUtil.UploadDoneHandle.Reset();
 
                 //    Type runCardClass = asm.GetType("AutoRunCard.RunCard");
                 //    object runCardObj = Activator.CreateInstance(runCardClass);
@@ -168,6 +178,7 @@
                 //}
 
                 CommonUtil.WriteLog(LogType.Inf, string.Format("妫�娴嬭褰曚笂浼犵粨鏉�,鍏辨湁{0}绗斿緟涓婁紶鏁版嵁锛屾垚鍔熶笂浼爗1}绗�", dtUpload.Rows.Count, uploadCount));
+                CommonUtil.UploadDoneHandle.Set();
             }
         }
 
@@ -234,11 +245,11 @@
                 int times = 1200 * 5;
                 while (uploadDataProcesser.IsAlive)
                 {
-                    if (!CommonUtil.UploadDataEnable)
-                    {
-                        Thread.Sleep(20);
-                        continue;
-                    }
+                    //if (!CommonUtil.UploadDataEnable)
+                    //{
+                    //    Thread.Sleep(20);
+                    //    continue;
+                    //}
 
                     //UploadData(asm);
                     UploadData();
diff --git a/MainForm.cs b/MainForm.cs
index 19f79e4..9355a5c 100644
--- a/MainForm.cs
+++ b/MainForm.cs
@@ -1075,18 +1075,34 @@
         #region 浜嬩欢
         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
         {
-            Stop();
-            SaveMeasureTTL();
-            Task.Factory.StartNew(() =>
+            try
             {
+                Stop();
+                SaveMeasureTTL();
+                //Task.Factory.StartNew(() =>
+                //{
                 SpotCheckData data = new SpotCheckData();
                 data.UpdateLastBreakProductQty(measureResultTTL.ProductQuntity);
                 DownTimeData downTimeData = new DownTimeData();
                 DownTime downTime = new DownTime() { BeginTime = DateTime.Now, Type = DownTimeType.Closed };
                 downTimeData.SaveDownTime(downTime);
-            });
+                //});
 
-            MessageHandler.MessageHandlerInstance.Close();
+                MessageHandler.MessageHandlerInstance.Close();
+            }
+            catch (Exception ex)
+            {
+                Trace.TraceError($"鐣岄潰鍏抽棴寮傚父锛歿ex.Message}");
+            }
+            finally
+            {
+                lock (CommonUtil.UploadDataLock)
+                {
+                    //CommonUtil.UploadDoneHandle.WaitOne();
+                    CommonUtil.UploadDataEnable = false;
+                    System.Environment.Exit(0);
+                }
+            }
         }
 
         private void btnConfig_Click(object sender, EventArgs e)
diff --git a/Program.cs b/Program.cs
index c0084e4..04ef6be 100644
--- a/Program.cs
+++ b/Program.cs
@@ -28,6 +28,7 @@
                 {
                     //聽绋嬪簭宸茬粡杩愯,鏄剧ず鎻愮ず鍚庨��鍑�
                     MessageBox.Show("搴旂敤绋嬪簭宸茬粡杩愯!");
+                    System.Environment.Exit(0);
                 }
             }
         }
diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs
index 8f75b42..90a2bfd 100644
--- a/Properties/AssemblyInfo.cs
+++ b/Properties/AssemblyInfo.cs
@@ -32,5 +32,5 @@
 // 鍙互鎸囧畾鎵�鏈夎繖浜涘�硷紝涔熷彲浠ヤ娇鐢ㄢ�滃唴閮ㄧ増鏈彿鈥濆拰鈥滀慨璁㈠彿鈥濈殑榛樿鍊硷紝
 // 鏂规硶鏄寜濡備笅鎵�绀轰娇鐢ㄢ��*鈥�:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.1.2")]
-[assembly: AssemblyFileVersion("1.0.1.2")]
+[assembly: AssemblyVersion("1.0.2.0")]
+[assembly: AssemblyFileVersion("1.0.2.0")]

--
Gitblit v1.8.0