From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 SimpleUtil/HMI.MQUtil/MQConnector.cs |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/SimpleUtil/HMI.MQUtil/MQConnector.cs b/SimpleUtil/HMI.MQUtil/MQConnector.cs
new file mode 100644
index 0000000..d93b519
--- /dev/null
+++ b/SimpleUtil/HMI.MQUtil/MQConnector.cs
@@ -0,0 +1,110 @@
+锘縰sing System;
+using System.Messaging;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace HMI.MQUtil
+{
+    public class MQConnector : IDisposable
+    {
+        private MessageQueue myQueue = null;
+
+        public MQConnector(string name)
+        {
+            string mqName = $".\\private$\\{name}";
+            //濡傛灉涓嶅瓨鍦╩qName娑堟伅闃熷垪锛屽垯鍒涘缓璇qName娑堟伅闃熷垪  
+            if (!MessageQueue.Exists(mqName))
+            {
+                MessageQueue.Create(mqName);
+
+                myQueue = new MessageQueue(mqName);
+                myQueue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);    //鍔犳潈闄�
+                myQueue.SetPermissions("ANONYMOUS LOGON", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
+            }
+            else
+            {
+                myQueue = new MessageQueue(mqName);
+            }
+        }
+
+        public MQConnector(string host, string name)
+        {
+            string mqName = $@"FormatName:Direct=TCP:{host}\Private$\{name}";
+            if (!MessageQueue.Exists(mqName))
+            {
+                MessageQueue.Create(mqName);
+
+                myQueue = new MessageQueue(mqName);
+                myQueue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);    //鍔犳潈闄�
+                myQueue.SetPermissions("ANONYMOUS LOGON", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
+            }
+            else
+            {
+                myQueue = new MessageQueue(mqName);
+            }
+        }
+
+        public void ClearAll()
+        {
+            myQueue?.Purge();
+        }
+
+        public void Close()
+        {
+            myQueue?.Close();
+        }
+
+
+
+        ManualResetEvent signal = new ManualResetEvent(false);
+
+        public void BeginReceive(Action<string> action)
+        {
+            Task.Run(() =>
+            {
+                //杩欓噷浣跨敤浜嗗鎵�,褰撴帴鏀舵秷鎭畬鎴愮殑鏃跺�欏氨鎵цMyReceiveCompleted鏂规硶
+                myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler((source, e) =>
+                {
+                    try
+                    {
+                        MessageQueue mq = (MessageQueue)source;
+                        //瀹屾垚鎸囧畾鐨勫紓姝ユ帴鏀舵搷浣�
+                        Message message = mq.EndReceive(e.AsyncResult);
+                        signal.Set();
+                        action(message.Body.ToString());
+                        mq.BeginReceive();
+                    }
+                    catch (MessageQueueException)
+                    {
+
+                    }
+                });
+                myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
+                myQueue.BeginReceive();
+                signal.WaitOne();
+            });
+        }
+
+        private void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
+        {
+
+        }
+
+        public bool SendMessage(string message)
+        {
+            myQueue.Send(message);
+            return true;
+        }
+
+        public bool SendUIMessage(int eventCode, int number, string message)
+        {
+            myQueue.Send($"{eventCode}|{number}|{message}");
+            return true;
+        }
+
+        public void Dispose()
+        {
+            Close();
+        }
+    }
+}

--
Gitblit v1.9.1