patrick.xu
2021-06-01 aefe9f2572eac7c61f6d2952593ec18a700dfcf0
1. 上传数据线程部分修改,界面关闭时退出所有相关线程
5个文件已修改
111 ■■■■■ 已修改文件
CommonUtil.cs 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DetectionDataUpload.cs 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
MainForm.cs 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Program.cs 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Properties/AssemblyInfo.cs 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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();
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();
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)
Program.cs
@@ -28,6 +28,7 @@
                {
                    // 程序已经运行,显示提示后退出
                    MessageBox.Show("应用程序已经运行!");
                    System.Environment.Exit(0);
                }
            }
        }
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")]