chr
2025-03-03 7b903bb86375e9df24a725288d1c876b8d2b0e61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//-----------------------------------------------------------------------
// <copyright file="MSMQHelper.cs" company="iPilotWhale">
//      Suzhou Linktron Systems Co.,Ltd . All rights reserved.
// </copyright>
// <author>温向超</author>
// <description>消息队列操作辅助类</description>
//-----------------------------------------------------------------------
using System;
using System.Messaging;
 
namespace HMI.Core.Util
{
    /// <summary>
    /// 消息队列操作类
    /// </summary>
    public class MSMQHelper
    {
        /// <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(string) });
            }
            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 MsgString)
        {
            try
            {
                this.msgQueue.Send(MsgString, 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 string MsgString)
        {
            try
            {
                MessageID = string.Empty;
                MsgString = string.Empty;
                TimeSpan Time = new TimeSpan(TimeOut*10000);
                Message MsgReceived = null;
                try
                {
                    MsgReceived = this.msgQueue.Receive(Time);
                }
                catch(Exception ex)
                {
                    // do nothing
                }
                
                if(MsgReceived!=null)
                {
                    MessageID = MsgReceived.Label;
                    MsgString = MsgReceived.Body.ToString();
                }
            }
            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);
        }
    }
}