C#使用Microsoft消息隊列(MSMQ)的示例詳解
寫在前面
Microsoft Message Queuing (MSMQ) 是在多個不同的應用之間實現(xiàn)相互通信的一種異步傳輸模式,相互通信的應用可以分布于同一臺機器上,也可以分布于相連的網(wǎng)絡空間中的任一位置。
使用消息隊列可以實現(xiàn)異步通訊,無需關心接收端是否在線,只需發(fā)出后就可以繼續(xù)處理后續(xù)的任務,通訊雙方也可以是不同的物理平臺,該機制還可以用來實現(xiàn)故障恢復。
在使用前,需要先確認該功能是否安裝和啟用,如未安裝可以參考以下操作步驟:
進入控制面板\所有控制面板項\程序和功能,點擊啟用或關閉Windows功能,勾選Microsoft消息隊列,確認并安裝。
消息隊列分為以下幾種,每種隊列的路徑表示形式如下:
公用隊列 MachineName\QueueName
專用隊列 MachineName\Private$\QueueName
日記隊列 MachineName\QueueName\Journal$
計算機日記隊列 MachineName\Journal$
計算機死信隊列 MachineName\Deadletter$
計算機事務性死信隊列 MachineName\XactDeadletter$
代碼實現(xiàn)
需要引用 System.Messaging 類庫
using System.Messaging;
發(fā)送端
public partial class Form1 : Form { MessageQueue queue; string path = ".\\Private$\\testQueue"; public Form1() { InitializeComponent(); CreateMessageQueue(); } private void button1_Click(object sender, EventArgs e) { SendMessage(this.textBox1.Text); } private void CreateMessageQueue() { if(MessageQueue.Exists(path)) { queue = new MessageQueue(path); } else { queue = MessageQueue.Create(path); } } private void SendMessage(string str) { var message = new System.Messaging.Message(); message.Formatter = new XmlMessageFormatter(new Type[]{ typeof(string)}); message.Body = str; queue.Send(message); MessageBox.Show("消息發(fā)送成功"); } private void SendMessage(Image image) { var message = new System.Messaging.Message(); message.Formatter = new BinaryMessageFormatter(); message.Body = image; queue.Send(message); MessageBox.Show("圖像發(fā)送成功"); } private void button2_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); open.Filter = "圖像文件|*.jpg;*.bmp;*.png"; open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); if(open.ShowDialog() == DialogResult.OK) { Image image = Bitmap.FromFile(open.FileName); SendMessage(image); } } private void button3_Click(object sender, EventArgs e) { MessageBox.Show(queue.GetAllMessages().Length.ToString()); } private void button4_Click(object sender, EventArgs e) { queue.Purge(); } }
接收端
public partial class Form1 : Form { MessageQueue queue; string path = ".\\Private$\\testQueue"; public Form1() { InitializeComponent(); CreateMessageQueue(); } private void button1_Click(object sender, EventArgs e) { RecvStringMessage(); } private void button2_Click(object sender, EventArgs e) { RecvImageMessage(); } private void CreateMessageQueue() { if (MessageQueue.Exists(path)) { queue = new MessageQueue(path); } else { queue = MessageQueue.Create(path); } } public void RecvStringMessage() { if (queue.GetAllMessages().Length == 0) { MessageBox.Show("消息隊列為空"); return; } var message = queue.Receive(); message.Formatter = new XmlMessageFormatter(new Type[] {typeof(string) }); MessageBox.Show(message.Body.ToString()); } public void RecvImageMessage() { if (queue.GetAllMessages().Length == 0) { MessageBox.Show("消息隊列為空"); return; } var message = queue.Receive(); message.Formatter = new BinaryMessageFormatter(); Image image = (Image)message.Body; Form form = new Form(); form.Width = 1024; form.Height = 768; PictureBox pbox = new PictureBox(); pbox.Width = 1024; pbox.Height = 768; pbox.SizeMode = PictureBoxSizeMode.Zoom; pbox.Image = image; form.Controls.Add(pbox); form.ShowDialog(); } }
調(diào)用示例
可以在計算機管理中查看到消息隊列的詳細信息,還未被接收的消息會在隊列消息中看到,被接收并消費后即自動刪除。
源碼參考:C#消息隊列MSMQ
以上就是C#使用Microsoft消息隊列(MSMQ)的示例詳解的詳細內(nèi)容,更多關于C# Microsoft消息隊列的資料請關注腳本之家其它相關文章!
相關文章
C#中const,readonly和static關鍵字的用法介紹
這篇文章介紹了C#中const,readonly和static關鍵字的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08WinForm實現(xiàn)讀取Resource中文件的方法
這篇文章主要介紹了WinForm實現(xiàn)讀取Resource中文件的方法,很實用的一個功能,需要的朋友可以參考下2014-08-08RSA密鑰--JAVA和C#的區(qū)別及聯(lián)系
這篇文章主要介紹了關于RSA密鑰事件JAVA和C#的區(qū)別及聯(lián)系,文章從RSA語法介紹開始展開詳細介紹了C#轉JAVA及JAVA轉C#,需要的小伙伴可以可以參考一下2021-10-10