程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#之消息隊列的簡要說明

C#之消息隊列的簡要說明

編輯:關於C#
 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Messaging ;
using System.Threading ;

namespace WinMsmq
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
static ManualResetEvent signal = new ManualResetEvent(false);
static ManualResetEvent pubRecvieSignal=new ManualResetEvent(false);
private System.Messaging.MessageQueue mq=null;
private readonly string qryname=@"";
private MessageQueue msq=null;
private MessageQueue pubMsq=null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button sendToPub;
private System.Windows.Forms.Button readFromPub;
private System.Windows.Forms.Button Creaet;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button delPubmsg;
private System.Windows.Forms.Button srPub;
private System.Data.SqlClient.SqlConnection sqlConnection1;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
this.qryname=System.Configuration.ConfigurationSettings.AppSettings["pubMsmqQryName"];
if(MessageQueue.Exists(qryname))
{
this.pubMsq=new MessageQueue(qryname,true);
}
else
{
this.pubMsq=System.Messaging.MessageQueue.Create(qryname,true);
}
this.richTextBox1.AppendText("==================/r/n");
this.richTextBox1.AppendText(this.pubMsq .QueueName +":公共隊列創建成功!/r/n");

//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button4 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.Creaet = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.srPub = new System.Windows.Forms.Button();
this.delPubmsg = new System.Windows.Forms.Button();
this.readFromPub = new System.Windows.Forms.Button();
this.sendToPub = new System.Windows.Forms.Button();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "SendMsg";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "CreateMsmq";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(8, 104);
this.button3.Name = "button3";
this.button3.TabIndex = 2;
this.button3.Text = "ReadMsg";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// richTextBox1
//
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBox1.Location = new System.Drawing.Point(0, 152);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(640, 188);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "richTextBox1";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Location = new System.Drawing.Point(16, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(144, 136);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "非事務";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button4);
this.groupBox2.Controls.Add(this.button7);
this.groupBox2.Controls.Add(this.Creaet);
this.groupBox2.Controls.Add(this.button6);
this.groupBox2.Controls.Add(this.button5);
this.groupBox2.Location = new System.Drawing.Point(184, 0);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(240, 136);
this.groupBox2.TabIndex = 5;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "事務性";
//
// button4
//
this.button4.Location = new System.Drawing.Point(168, 80);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(56, 48);
this.button4.TabIndex = 5;
this.button4.Text = "CreateMsmqWithTran";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(16, 104);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(136, 23);
this.button7.TabIndex = 4;
this.button7.Text = "ClearMsgOfRichTxtBox";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// Creaet
//
this.Creaet.Location = new System.Drawing.Point(144, 32);
this.Creaet.Name = "Creaet";
this.Creaet.Size = new System.Drawing.Size(80, 40);
this.Creaet.TabIndex = 3;
this.Creaet.Text = "異步讀取消息";
this.Creaet.Click += new System.EventHandler(this.Read);
//
// button6
//
this.button6.Location = new System.Drawing.Point(16, 72);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(104, 23);
this.button6.TabIndex = 2;
this.button6.Text = "ReadMsgTran";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(16, 32);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(104, 24);
this.button5.TabIndex = 1;
this.button5.Text = "SendMsgTran";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// groupBox3
//
this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox3.Controls.Add(this.srPub);
this.groupBox3.Controls.Add(this.delPubmsg);
this.groupBox3.Controls.Add(this.readFromPub);
this.groupBox3.Controls.Add(this.sendToPub);
this.groupBox3.Location = new System.Drawing.Point(432, 0);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(208, 136);
this.groupBox3.TabIndex = 6;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "公共隊列";
//
// srPub
//
this.srPub.Location = new System.Drawing.Point(144, 72);
this.srPub.Name = "srPub";
this.srPub.Size = new System.Drawing.Size(56, 48);
this.srPub.TabIndex = 4;
this.srPub.Text = "檢索公共隊列";
this.srPub.Click += new System.EventHandler(this.srPub_Click);
//
// delPubmsg
//
this.delPubmsg.Location = new System.Drawing.Point(8, 96);
this.delPubmsg.Name = "delPubmsg";
this.delPubmsg.Size = new System.Drawing.Size(112, 24);
this.delPubmsg.TabIndex = 3;
this.delPubmsg.Text = "DeletePubMsmg";
this.delPubmsg.Click += new System.EventHandler(this.delPubmsg_Click);
//
// readFromPub
//
this.readFromPub.Location = new System.Drawing.Point(8, 64);
this.readFromPub.Name = "readFromPub";
this.readFromPub.Size = new System.Drawing.Size(128, 24);
this.readFromPub.TabIndex = 2;
this.readFromPub.Text = "ReadMsgFromPubMsmg";
this.readFromPub.Click += new System.EventHandler(this.readFromPub_Click);
//
// sendToPub
//
this.sendToPub.Location = new System.Drawing.Point(8, 32);
this.sendToPub.Name = "sendToPub";
this.sendToPub.Size = new System.Drawing.Size(144, 24);
this.sendToPub.TabIndex = 1;
this.sendToPub.Text = "SendMsgToPubMsmq";
this.sendToPub.Click += new System.EventHandler(this.sendToPub_Click);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=DHZ;packet size=4096;integrated security=SSPI;data source=/"DHZ//DHZ" +
"/";persist security info=False;initial catalog=Northwind";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(648, 349);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.richTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{

if(MessageQueue.Exists(@"./private$/dhzOrderMgmt"))
{
this.mq=new MessageQueue(@"./private$/dhzOrderMgmt");
}
else
{
this.mq=MessageQueue.Create(@"./private$/dhzOrderMgmt",false);
}
this.richTextBox1.AppendText("==================/r/n");
this.richTextBox1.AppendText(mq.QueueName +":創建成功!/r/n");


}
private void button1_Click(object sender, System.EventArgs e)
{
Od od=new Od("代漢章","南昌",DateTime.Now.ToLongTimeString ());
if(this.mq.Transactional)
{
this.mq.Send(od,new System.Messaging.MessageQueueTransaction ());
}
else
{
this.mq.Send(od);
}
this.richTextBox1.AppendText("發送成功!/r/n");
}
private void button2_Click(object sender, System.EventArgs e)
{

if(MessageQueue.Exists(@"./private$/dhzOrderMgmt"))
{
this.mq=new MessageQueue(@"./private$/dhzOrderMgmt");
}
else
{
this.mq=MessageQueue.Create(@"./private$/dhzOrderMgmt",false);
}
this.richTextBox1.AppendText("==================/r/n");
this.richTextBox1.AppendText(mq.QueueName +":創建成功!/r/n");


}
private void button3_Click(object sender, System.EventArgs e)
{
if(this.mq!=null)
{
System.Messaging.MessageEnumerator msg=this.mq.GetMessageEnumerator ();
while(msg.MoveNext ())
{
System.Messaging .Message oc=msg.Current ;
Od od=oc.Body as Od ;
this.richTextBox1.AppendText(od.Name +":"+od.City +":"+od.Time +";/r/n");

}

}
}
private void button4_Click(object sender, System.EventArgs e)
{
if(MessageQueue.Exists(@"./private$/dhzTrans"))
{
this.msq =new MessageQueue(@"./private$/dhzTrans");
}
else
{
this.msq=MessageQueue.Create(@"./private$/dhzTrans",true);
}
this.richTextBox1.AppendText("==================/r/n");
this.richTextBox1.AppendText(msq.QueueName +":創建成功!/r/n");

}
private void button5_Click(object sender, System.EventArgs e)
{

this.msq=new MessageQueue(@"./private$/dhzTrans");
if(this.msq.Transactional)
{
Od od=new Od("萬卿","景德鎮",DateTime.Now.ToString("u"));
MessageQueueTransaction myTransaction = new
MessageQueueTransaction();
myTransaction.Begin();
msq.Send(od,myTransaction);
myTransaction.Commit();
this.richTextBox1.AppendText("OK!發送成功:"+od.Time+"/r/n" );
}

}
private void button6_Click(object sender, System.EventArgs e)
{
this.msq=new MessageQueue(@"./private$/dhzTrans");
if(this.msq .Transactional )
{
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
this.msq.Formatter=new System.Messaging.XmlMessageFormatter (new Type[]{Type.GetType("WinMsmq.Od" )});
myTransaction.Begin();
System.Messaging.Message ms=msq.Receive(myTransaction);
Od od=ms.Body as Od ;
this.richTextBox1.AppendText(od.Name +":"+od.City +":"+od.Time +";/r/n");
myTransaction.Commit();
this.richTextBox1.AppendText("OK!發送成功/r/n");
}
}
private void Read(object sender, System.EventArgs e)
{
this.msq=new MessageQueue(@"./private$/dhzTrans");
if(this.msq.Transactional)
{
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
msq.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
this.msq.Formatter=new System.Messaging.XmlMessageFormatter (new Type[]{Type.GetType("WinMsmq.Od" )});
myTransaction.Begin();
msq.BeginReceive();
Form1.signal.WaitOne ();
myTransaction.Commit ();
}
}
private void MyReceiveCompleted(Object source,ReceiveCompletedEventArgs asyncResult)
{
try
{
MessageQueue mq = (MessageQueue)source;
System.Messaging .Message m = mq.EndReceive(asyncResult.AsyncResult);
Form1.signal.Set ();
Od od=m.Body as Od ;
this.richTextBox1.AppendText ("Read:"+od.Name +":"+od.City +":"+od.Time +"/r/n"+"插入數據庫完成/r/n");
mq.BeginReceive();
}
catch(MessageQueueException c)
{
this.richTextBox1.AppendText("Error:"+c.Message +"/r/n");
this.button5.Enabled=false;
}
// Handle other exceptions.


}
private void button7_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Clear ();
}
private void delPubmsg_Click(object sender, System.EventArgs e)
{
//刪除創建的公共隊列
System.Messaging.MessageQueue.Delete (qryname);
}
private void srPub_Click(object sender, System.EventArgs e)
{
MessageQueue[] mq=System.Messaging.MessageQueue.GetPublicQueuesByMachine ("dhz");
for(int i=0;i<mq.Length ;i++)
{
this.richTextBox1.AppendText(mq[i].QueueName +":/r/n");
}

}
private void sendToPub_Click(object sender, System.EventArgs e)
{
if(this.pubMsq==null)
this.pubMsq=new MessageQueue(this.qryname );
if(this.pubMsq.Transactional )
{
Od od=new Od("萬卿","景德鎮",DateTime.Now.ToString("u"));
MessageQueueTransaction myTransaction = new
MessageQueueTransaction();
myTransaction.Begin();
pubMsq.Send(od,myTransaction);
myTransaction.Commit();
}
}
private void readFromPub_Click(object sender, System.EventArgs e)
{
this.pubMsq=new MessageQueue(this.qryname );
if(this.pubMsq.Transactional)
{
MessageQueueTransaction myTransaction = new MessageQueueTransaction();
pubMsq.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
this.pubMsq.Formatter=new System.Messaging.XmlMessageFormatter (new Type[]{Type.GetType("WinMsmq.Od" )});
myTransaction.Begin();
pubMsq.BeginReceive();
Form1.signal.WaitOne ();
myTransaction.Commit ();
}

}

 

}
}
/////////////////////////================
public class Od
{
public Od()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public Od(string na,string cty,string timestr)
{
this.name=na;
this.city=cty;
this.time=timestr;

}
private string time="";
private string name;
private string city;
public string Name
{
get{return this.name ;}
set{this.name=value;}
}
public string City
{
get{return this.city ;}
set{this.city=value;}
}
public string Time
{
get{return this.time ;}
set{this.time=value;}
}
}
=======================App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--ComputerName-->
<!--dhz:是域控制器所在的計算機名:-->
<add key="QcName" value="dhz/FormatName:Public=620B1BFB-18E9-4d80-B869-FF6FB2DFDD08"></add>
</appSettings>
</configuration>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved