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 [email protected]"";
 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>