返回首页
当前位置: 主页 > 编程语言 > C#教程 >

C#之消息队列的简要说明

时间:2015-01-31 23:09来源:电脑教程学习网 www.etwiki.cn 编辑:admin

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>

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):c# c#技巧 c#实例教程 c#源代码 c#基础教程
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐内容