From 440a6da24fbde3e1088710d9d1e5a874f93f462c Mon Sep 17 00:00:00 2001
From: nodyang <yangw@outlook.com>
Date: Mon, 28 Aug 2023 15:20:13 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8E=8B=E5=8A=9B=E7=AE=A1=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Mesnac.Action.ChemicalWeighing/LjHelp.cs  |   8 ++
 .../LjPressure/FrmPressureUpdate.Designer.cs  |   3 +-
 .../LjPressure/FrmPressureUpdate.cs           |  15 ++-
 .../LjPressure/PressurePLC.cs                 |   3 +-
 .../LjPressure/UpAction.cs                    |  11 ++
 .../Loading.Designer.cs                       |  66 ++++++++++
 .../Mesnac.Action.ChemicalWeighing/Loading.cs |  68 ++++++++++
 .../Loading.resx                              | 120 ++++++++++++++++++
 .../LoadingHelper.cs                          |  81 ++++++++++++
 .../Mesnac.Action.ChemicalWeighing.csproj     |  11 ++
 10 files changed, 381 insertions(+), 5 deletions(-)
 create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjHelp.cs
 create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.Designer.cs
 create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.cs
 create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.resx
 create mode 100644 Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LoadingHelper.cs

diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjHelp.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjHelp.cs
new file mode 100644
index 0000000..706f323
--- /dev/null
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjHelp.cs
@@ -0,0 +1,8 @@
+namespace Mesnac.Action.ChemicalWeighing
+{
+    public static class LjHelp
+    {
+        //开门狗
+        public static int WatchDog=>BasePlcHelper.Instance.WatchDog.LastValue.ToInt();
+    }
+}
\ No newline at end of file
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.Designer.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.Designer.cs
index b065df0..b493033 100644
--- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.Designer.cs
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.Designer.cs
@@ -138,10 +138,9 @@ namespace Mesnac.Action.ChemicalWeighing.LjPressure
             this.Controls.Add(this.label1);
             this.ForeColor = System.Drawing.SystemColors.ControlText;
             this.Name = "FrmPressureUpdate";
-            this.Text = "需改压力参数";
+            this.Text = "修改压力参数";
             this.ResumeLayout(false);
             this.PerformLayout();
-
         }
 
         private System.Windows.Forms.ComboBox Alarm;
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.cs
index b6d9fc8..6c600a1 100644
--- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.cs
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/FrmPressureUpdate.cs
@@ -18,6 +18,9 @@ namespace Mesnac.Action.ChemicalWeighing.LjPressure
         private int _id;
         public FrmPressureUpdate(int id) : this()
         {
+            
+            LoadingHelper.ShowLoadingScreen();
+            
             _id = id;
             
             IList<MyNameValue> ls = new List<MyNameValue>();
@@ -34,7 +37,15 @@ namespace Mesnac.Action.ChemicalWeighing.LjPressure
             this.Alarm.DataSource = ls;
             Alarm.ValueMember = "Id";
             Alarm.DisplayMember = "Name";
-         
+
+            var i = BasePlcHelper.Instance.WatchDog.LastValue.ToInt();
+            if (i == 0)
+            {
+                LoadingHelper.CloseForm();
+                MessageBox.Show("PLC通讯失败");
+                return;
+            }
+
             var v = PressurePLC.UpdateFromPlc(id);
             if (null!=v)
             {
@@ -55,6 +66,8 @@ namespace Mesnac.Action.ChemicalWeighing.LjPressure
             {
                 MessageBox.Show("获取PLC 异常请联系管理员");
             }
+            
+            LoadingHelper.CloseForm();
         }
 
         private void btnCancel_Click(object sender, EventArgs e)
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/PressurePLC.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/PressurePLC.cs
index 6d4d75e..f9694bc 100644
--- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/PressurePLC.cs
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/PressurePLC.cs
@@ -45,8 +45,7 @@ namespace Mesnac.Action.ChemicalWeighing.LjPressure
                 var basePlcHelper = GetPlcHelper;
                 basePlcHelper.PlcWriteFloatByDataKey(basePlcHelper.topPressure_V1_LowLimit, view.LowLimit);
                 basePlcHelper.PlcWriteFloatByDataKey(basePlcHelper.topPressure_V1_HighLimit, view.HighLimit);
-                // basePlcHelper.PlcWriteByDataKey(basePlcHelper.topPressure_V1_Alarm,
-                //     new object[] { Convert.ToBoolean(view.Alarm) });
+              
             }
         }
         
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/UpAction.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/UpAction.cs
index 07b500d..a854bb9 100644
--- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/UpAction.cs
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LjPressure/UpAction.cs
@@ -30,6 +30,15 @@ namespace Mesnac.Action.ChemicalWeighing.LjPressure
 
         private void DoWord()
         {
+            LoadingHelper.ShowLoadingScreen();
+            var i = BasePlcHelper.Instance.WatchDog.LastValue.ToInt();
+            if (i == 0)
+            {
+                LoadingHelper.CloseForm();
+                MessageBox.Show("PLC通讯失败");
+                return;
+            }
+            
             var pressureSettingViews = PressurePLC.UpdateFromPlc();
             foreach (var v in pressureSettingViews)
             {
@@ -38,6 +47,8 @@ namespace Mesnac.Action.ChemicalWeighing.LjPressure
                 DBHelp.ExecuteNonQuery(sql);
             }
             
+            LoadingHelper.CloseForm();
+            
             
             if (OnHandler != null)
             {
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.Designer.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.Designer.cs
new file mode 100644
index 0000000..bb41fec
--- /dev/null
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.Designer.cs
@@ -0,0 +1,66 @@
+using System.ComponentModel;
+using System.Drawing;
+using System.Windows.Forms;
+
+namespace Mesnac.Action.ChemicalWeighing
+{
+    partial class Loading
+    {
+        /// <summary>
+        /// Required designer variable.
+        /// </summary>
+        private IContainer components = null;
+
+        /// <summary>
+        /// Clean up any resources being used.
+        /// </summary>
+        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+
+            base.Dispose(disposing);
+        }
+
+        #region Windows Form Designer generated code
+
+        /// <summary>
+        /// Required method for Designer support - do not modify
+        /// the contents of this method with the code editor.
+        /// </summary>
+        private void InitializeComponent()
+        {
+            this.label2 = new System.Windows.Forms.Label();
+            this.SuspendLayout();
+            // 
+            // label2
+            // 
+            this.label2.AutoSize = true;
+            this.label2.Location = new System.Drawing.Point(122, 40);
+            this.label2.Name = "label2";
+            this.label2.Size = new System.Drawing.Size(65, 12);
+            this.label2.TabIndex = 0;
+            this.label2.Text = "数据加载中";
+            // 
+            // Loading
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.ClientSize = new System.Drawing.Size(299, 72);
+            this.Controls.Add(this.label2);
+            this.Name = "Loading";
+            this.Text = "Loading";
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
+        }
+
+        #endregion
+
+        private Label label1;
+        private Label label2;
+    }
+}
\ No newline at end of file
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.cs
new file mode 100644
index 0000000..cbb43a9
--- /dev/null
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Windows.Forms;
+
+namespace Mesnac.Action.ChemicalWeighing
+{
+    public partial class Loading : Form
+    {
+        public Loading()
+        {
+            InitializeComponent();
+        }
+
+        private void loading_Load(object sender, EventArgs e)
+        {
+            //this.BackColor = Color.Transparent;
+            this.Opacity = 1;
+
+        }
+
+        /// <summary>
+        /// 关闭命令
+        /// </summary>
+        public void CloseOrder()
+        {
+            if (this.InvokeRequired)
+            {
+                //这里利用委托进行窗体的操作,避免跨线程调用时抛异常,后面给出具体定义
+                CONSTANTDEFINE.SetUISomeInfo UIinfo = new CONSTANTDEFINE.SetUISomeInfo(new System.Action(() =>
+                {
+                    while (!this.IsHandleCreated)
+                    {
+                        ;
+                    }
+                    
+                    if (!this.IsDisposed)
+                    {
+                        this.Dispose();
+                    }
+
+                }));
+                this.Invoke(UIinfo);
+            }
+            else
+            {
+                if (this.IsDisposed)
+                    return;
+                if (!this.IsDisposed)
+                {
+                    this.Dispose();
+                }
+            }
+        }
+
+        private void loading_FormClosing(object sender, FormClosingEventArgs e)
+        {
+            if (!this.IsDisposed)
+            {
+                this.Dispose(true);
+            }
+
+        }
+    }
+
+    class CONSTANTDEFINE
+    {
+        public delegate void SetUISomeInfo();
+    }
+}
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.resx b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.resx
new file mode 100644
index 0000000..1af7de1
--- /dev/null
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Loading.resx
@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+</root>
\ No newline at end of file
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LoadingHelper.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LoadingHelper.cs
new file mode 100644
index 0000000..f5a871a
--- /dev/null
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/LoadingHelper.cs
@@ -0,0 +1,81 @@
+using System;
+using System.Threading;
+
+namespace Mesnac.Action.ChemicalWeighing
+{
+   public class LoadingHelper
+    {
+        #region 相关变量定义
+        /// <summary>
+        /// 定义委托进行窗口关闭
+        /// </summary>
+        private delegate void CloseDelegate();
+        private static Loading loadingForm;
+        private static readonly Object syncLock = new Object();  //加锁使用
+ 
+        #endregion
+ 
+        //private LoadingHelper()
+        //{
+ 
+        //}
+ 
+        /// <summary>
+        /// 显示loading框
+        /// </summary>
+        public static void ShowLoadingScreen()
+        {
+            // Make sure it is only launched once.
+            if (loadingForm != null)
+                return;
+            Thread thread = new Thread(new ThreadStart(LoadingHelper.ShowForm));
+            thread.IsBackground = true;
+            thread.SetApartmentState(ApartmentState.STA);
+            thread.Start();
+ 
+        }
+        /// <summary>
+        /// 显示窗口
+        /// </summary>
+        private static void ShowForm()
+        {
+            if (loadingForm != null)
+            {
+                loadingForm.CloseOrder();
+                loadingForm = null;
+            }
+            loadingForm = new Loading();
+            loadingForm.TopMost = true;
+            loadingForm.ShowDialog();
+        }
+        /// <summary>
+        /// 关闭窗口
+        /// </summary>
+        public static void CloseForm()
+        {
+            Thread.Sleep(50); //可能到这里线程还未起来,所以进行延时,可以确保线程起来,彻底关闭窗口
+            if (loadingForm != null)
+            {
+                lock (syncLock)
+                {
+                    Thread.Sleep(50);
+                    if (loadingForm != null)
+                    {
+                        Thread.Sleep(50);  //通过三次延时,确保可以彻底关闭窗口
+                        loadingForm.Invoke(new CloseDelegate(LoadingHelper.CloseFormInternal));
+                    }
+                }
+            }
+        }
+        /// <summary>
+        /// 关闭窗口,委托中使用
+        /// </summary>
+        private static void CloseFormInternal()
+        {
+            loadingForm.CloseOrder();
+            loadingForm = null;
+        }
+    }
+
+
+}
\ No newline at end of file
diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj
index b8f7ae4..a36cb46 100644
--- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj
+++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj
@@ -335,6 +335,7 @@
     <Compile Include="LjElectrical\InitDbAction.cs" />
     <Compile Include="LjElectrical\UpAction.cs" />
     <Compile Include="LjElectrical\UpdateAction.cs" />
+    <Compile Include="LjHelp.cs" />
     <Compile Include="LjMaterial\DBHelp.cs" />
     <Compile Include="LjMaterial\DelAction.cs" />
     <Compile Include="LjMaterial\FrmMaterial.cs">
@@ -502,6 +503,13 @@
     <Compile Include="LjWeight\UpdateAction.cs" />
     <Compile Include="LjWeight\WeightSettingPlc.cs" />
     <Compile Include="LjWeight\WeightSettingView.cs" />
+    <Compile Include="Loading.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="Loading.Designer.cs">
+      <DependentUpon>Loading.cs</DependentUpon>
+    </Compile>
+    <Compile Include="LoadingHelper.cs" />
     <Compile Include="MaterialManage\BarcodeReport.cs">
       <SubType>Component</SubType>
     </Compile>
@@ -832,6 +840,9 @@
     <EmbeddedResource Include="LjWeight\FrmWeight.resx">
       <DependentUpon>FrmWeight.cs</DependentUpon>
     </EmbeddedResource>
+    <EmbeddedResource Include="Loading.resx">
+      <DependentUpon>Loading.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="MaterialManage\BarcodeReport.resx">
       <DependentUpon>BarcodeReport.cs</DependentUpon>
     </EmbeddedResource>