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.Simple.Util/MSMQHelper.cs | 148 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 148 insertions(+), 0 deletions(-)
diff --git a/SimpleUtil/HMI.Simple.Util/MSMQHelper.cs b/SimpleUtil/HMI.Simple.Util/MSMQHelper.cs
new file mode 100644
index 0000000..06d9f04
--- /dev/null
+++ b/SimpleUtil/HMI.Simple.Util/MSMQHelper.cs
@@ -0,0 +1,148 @@
+锘縰sing System;
+using System.Messaging;
+
+namespace HMI.Simple.Util
+{
+
+ public class MQAlarm
+ {
+ public string Source { get; set; }
+ public string Content { get; set; }
+ public int Level { get; set; }
+ }
+
+
+ public class MSMQHelper : IDisposable
+ {
+ /// <summary>
+ /// 娑堟伅闃熷垪鍚嶇О
+ /// </summary>
+ private string msmqName = string.Empty;
+
+ /// <summary>
+ /// 娑堟伅闃熷垪璺緞
+ /// </summary>
+ private string msmqPath = string.Empty;
+
+ /// <summary>
+ /// 瑕佹搷浣滅殑娑堟伅闃熷垪
+ /// </summary>
+ private MessageQueue msgQueue = null;
+
+ /// <summary>
+ /// 鏋勯�犲嚱鏁�
+ /// </summary>
+ /// <param name="Name">娑堟伅闃熷垪鍚嶇О</param>
+ public MSMQHelper(string Name)
+ {
+ try
+ {
+ this.msmqName = Name;
+ this.msmqPath = ".\\private$\\" + this.msmqName;
+ if (MessageQueue.Exists(this.msmqPath))
+ {
+ // 濡傛灉褰撳墠娑堟伅闃熷垪宸茬粡瀛樺湪
+ this.msgQueue = new MessageQueue(this.msmqPath);
+ }
+ else
+ {
+ // 濡傛灉娑堟伅闃熷垪涓嶅瓨鍦紝鍒欏垱寤烘秷鎭鍨�
+ this.msgQueue = MessageQueue.Create(this.msmqPath);
+ }
+ this.msgQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MQAlarm) });
+ }
+ catch (Exception ex)
+ {
+ throw new Exception(ex.Message);
+ }
+ }
+
+ /// <summary>
+ /// 鍙戦�佹秷鎭�
+ /// </summary>
+ /// <param name="MessageID">娑堟伅ID</param>
+ /// <param name="MsgString">瑕佸彂閫佺殑瀛楃涓�</param>
+ public void Send(string MessageID, string source, string MsgString, int level)
+ {
+ try
+ {
+ this.msgQueue.Send(new MQAlarm { Source = source, Content = MsgString, Level = level }, MessageID);
+ }
+ catch (Exception ex)
+ {
+ throw new Exception(ex.Message);
+ }
+ }
+
+ /// <summary>
+ /// 鎺ユ敹娑堟伅闃熷垪涓殑娑堟伅
+ /// </summary>
+ /// <param name="TimeOut">瓒呮椂鏃堕棿(ms)</param>
+ /// <param name="MessageID">娑堟伅ID</param>
+ /// <param name="MsgString">娑堟伅鍐呭</param>
+ public void Receive(long TimeOut, out string MessageID, out MQAlarm alarm)
+ {
+ try
+ {
+ MessageID = string.Empty;
+ alarm = null;
+ TimeSpan Time = new TimeSpan(TimeOut);
+ Message MsgReceived = null;
+ try
+ {
+ MsgReceived = this.msgQueue.Receive(Time);
+ }
+ catch
+ {
+ // do nothing
+ }
+
+ if (MsgReceived != null)
+ {
+ MessageID = MsgReceived.Label;
+ alarm = (MQAlarm)MsgReceived.Body;
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new Exception("鎺ユ敹娑堟伅澶辫触:" + ex.Message);
+ }
+ }
+
+ /// <summary>
+ /// 娓呴櫎娑堟伅闃熷垪涓殑娑堟伅
+ /// </summary>
+ public void Clear()
+ {
+ try
+ {
+ this.msgQueue.Purge();
+ }
+ catch (Exception ex)
+ {
+ throw new Exception("娓呴櫎娑堟伅闃熷垪澶辫触:" + ex.Message);
+ }
+ }
+
+ /// <summary>
+ /// 鍏抽棴褰撳墠闃熷垪
+ /// </summary>
+ public void Close()
+ {
+ this.msgQueue.Close();
+ }
+
+ /// <summary>
+ /// 鍒犻櫎褰撳墠闃熷垪
+ /// </summary>
+ public void Delete()
+ {
+ MessageQueue.Delete(this.msmqPath);
+ }
+
+ public void Dispose()
+ {
+ this.Close();
+ }
+ }
+}
--
Gitblit v1.9.1