From 8a3ab64a65da226636743be07c4bee63f50da25d Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期六, 06 三月 2021 11:35:20 +0800
Subject: [PATCH] 1. 解决软件异常崩溃问题

---
 src/Bro.UI.Config/MainFrm.cs |  152 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 127 insertions(+), 25 deletions(-)

diff --git a/src/Bro.UI.Config/MainFrm.cs b/src/Bro.UI.Config/MainFrm.cs
index e7940f5..1d27f20 100644
--- a/src/Bro.UI.Config/MainFrm.cs
+++ b/src/Bro.UI.Config/MainFrm.cs
@@ -74,10 +74,10 @@
             //});
 
             m_deserializeMenuFrm = new DeserializeDockContent(GetMenuFromPersistString);
-            LoadLayoutFromXML(m_deserializeMenuFrm);
-
             m_deserializeDeviceRunFrm = new DeserializeDockContent(GetAllFormPersistString);
-            _allMenuLoadDoneHandle.Set();
+
+            //LoadLayoutFromXML(m_deserializeMenuFrm);
+            //_allMenuLoadDoneHandle.Set();
 
             Text = SettingHelper.GetProgramDescription();
             string iconPath = SettingHelper.GetProgramIcon();
@@ -90,7 +90,9 @@
         private void RegisterEvent(MenuFrmBase dockFrm)
         {
             dockFrm.OnUploadProcess = DockFrm_OnUploadProcess;
-            dockFrm.OnLogMsgOutput = DockFrm_OnLogMsgOutput;
+
+            dockFrm.OnLogMsgOutput -= DockFrm_OnLogMsgOutput;
+            dockFrm.OnLogMsgOutput += DockFrm_OnLogMsgOutput;
 
             //if (dockFrm is FrmDevices)
             //{
@@ -155,21 +157,32 @@
                 CloseAllDeviceFrm();
                 LoadDevices();
 
-                foreach (var dock in dockPanelMain.Contents)
+                try
                 {
-                    MenuFrmBase m = dock as MenuFrmBase;
-
-                    if (m != null && m.Id != frmId)
+                    foreach (var dock in dockPanelMain.Contents)
                     {
-                        m.DownloadProcess(process);
+                        MenuFrmBase m = dock as MenuFrmBase;
+
+                        if (m != null && m.Id != frmId)
+                        {
+                            m.DownloadProcess(process);
+                        }
                     }
+                }
+                catch (Exception ex)
+                {
                 }
             });
         }
 
-        private void Process_OnAlarmUpdate(string alarmMsg)
+        private async void Process_OnAlarmUpdate(string alarmMsg)
         {
-            tsslWarning.Text = alarmMsg;
+            this.BeginInvoke(new Action(() =>
+            {
+                tsslWarning.Text = alarmMsg;
+            }));
+
+            await Task.Delay(100);
         }
 
         private ToolStripMenuItem GetMatchNode(ToolStripItemCollection items, string parentMenuCode)
@@ -247,14 +260,95 @@
 
         private void MainFrm_Load(object sender, EventArgs e)
         {
+            LoadLayoutFromXML(m_deserializeMenuFrm);
+            _allMenuLoadDoneHandle.Set();
+
             AdvancedPwdFrm.OnLoginOK = OnLoginOK;
 
-            SpinWait wait = new SpinWait();
-            while (_process == null)
-            {
-                wait.SpinOnce();
-            }
+            //SpinWait wait = new SpinWait();
+            //while (_process == null)
+            //{
+            //    wait.SpinOnce();
+            //}
+
+            LoadProcess();
+
             LoadLayoutFromXML(m_deserializeDeviceRunFrm);
+
+            LoadProcess(false);
+        }
+
+        private List<string> LoadProcessCode()
+        {
+            var systemProcessCodes = ProcessFactory.GetProcessCodes();
+            var avaiableProcessCodes = SettingHelper.GetProcessCodes();
+
+            List<string> pCodes = new List<string>();
+
+            if (avaiableProcessCodes.Count > 0)
+            {
+                pCodes = avaiableProcessCodes.Intersect(systemProcessCodes).ToList();
+            }
+            else
+            {
+                pCodes = systemProcessCodes;
+            }
+
+            return pCodes;
+        }
+
+        private List<string> LoadProductionCode()
+        {
+            return SettingHelper.GetProductionCodes();
+        }
+
+        private void LoadProcess(bool isInitialProcess = true)
+        {
+            if (isInitialProcess)
+            {
+                _process = ProcessFactory.CreateStationProcess(LoadProcessCode()[0], LoadProductionCode()[0], out string msg);
+
+                if (!string.IsNullOrWhiteSpace(msg))
+                {
+                    _process = null;
+                    //LogAsync(DateTime.Now, "Process鍒涘缓澶辫触", $"{msg}");
+                    throw new ProcessException($"Process鍒涘缓澶辫触,{msg}", null, ExceptionLevel.Fatal);
+                }
+
+
+                _process.InitialProcess("");
+            }
+            _process.OnLog -= OnProcessLogOutput;
+            _process.OnLog += OnProcessLogOutput;
+
+            _process.OnAlarmUpdate -= Process_OnAlarmUpdate;
+            _process.OnAlarmUpdate += Process_OnAlarmUpdate;
+
+            //CloseAllDeviceFrm();
+            LoadDevices();
+
+            try
+            {
+                foreach (var dock in dockPanelMain.Contents)
+                {
+                    //MenuFrmBase m = dock as MenuFrmBase;
+
+                    //m.DownloadProcess(_process);
+
+                    if (dock is MenuFrmBase menuFrm)
+                    {
+                        menuFrm.DownloadProcess(_process);
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+            }
+        }
+
+        private void OnProcessLogOutput(DateTime dt, string prefix, string msg)
+        {
+            DockFrm_OnLogMsgOutput(new LogMsg(dt, prefix, msg));
         }
 
         private void CloseAllDocuments()
@@ -277,6 +371,11 @@
 
         private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
         {
+            try
+            {
+                _process?.Close();
+            }
+            catch { }
             CloseAllDocuments();
         }
 
@@ -310,7 +409,7 @@
 
         private void OnLoginOK(bool isLogin)
         {
-            IsLogin = true;
+            IsLogin = isLogin;
         }
         #endregion
 
@@ -411,6 +510,9 @@
                 }).ToList().ForEach(u =>
                 {
                     if (u == null)
+                        return;
+
+                    if (_process.DeviceCollection.Any(d => d.Id == u.Device.Id))
                         return;
 
                     u.DockPanel = null;
@@ -535,10 +637,10 @@
 
                 dockFrm.Text = desc[2];
 
-                if (_process != null)
-                {
-                    dockFrm.DownloadProcess(_process);
-                }
+                //if (_process != null)
+                //{
+                //    dockFrm.DownloadProcess(_process);
+                //}
 
                 dockFrm.SetLoginStatus(IsLogin);
 
@@ -573,10 +675,10 @@
 
                 dockFrm.Text = desc[2];
 
-                if (_process != null)
-                {
-                    dockFrm.DownloadProcess(_process);
-                }
+                //if (_process != null)
+                //{
+                //    dockFrm.DownloadProcess(_process);
+                //}
 
                 dockFrm.SetLoginStatus(IsLogin);
 

--
Gitblit v1.8.0