⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 03msmqdemo.aspx.cs

📁 C#下的消息处理DEMO,很完整,对于初学者了解消息处理机制很有帮助.
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Messaging;
using System.Threading ; 
namespace ASPNETMSMQ
{
	/// <summary>
	/// MSMQDemo 的摘要说明。
	/// </summary>
	public class MSMQDemo : System.Web.UI.Page
	{
		static ManualResetEvent signal = new ManualResetEvent(false); 
		static ManualResetEvent pubRecvieSignal=new ManualResetEvent(false); 
		static private System.Messaging.MessageQueue mq=null; 
		static private string qryname=@""; 
		static private MessageQueue msq=null;

		protected System.Web.UI.WebControls.Button Button6;
		protected System.Web.UI.WebControls.Button Button7;
		protected System.Web.UI.WebControls.Button Button8;
		protected System.Web.UI.WebControls.Button btnCreate;
		
		protected System.Web.UI.WebControls.TextBox tbOut;
		protected System.Web.UI.WebControls.Button btnSend;
		protected System.Web.UI.WebControls.Button btnRead;
		protected System.Web.UI.WebControls.Button btnYibu;
		protected System.Web.UI.WebControls.Button btnSendToPub;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.Button Button2;
		protected System.Web.UI.WebControls.Button Button3;
		protected System.Web.UI.WebControls.Button Button4; 
		private MessageQueue pubMsq=null; 

		private void Page_Load(object sender, System.EventArgs e)
		{
			
			if(MessageQueue.Exists(@".\private$\dhzOrderMgmt")) 
			{ 
				mq=new MessageQueue(@".\private$\dhzOrderMgmt"); 
			} 
			else 
			{ 
				mq=MessageQueue.Create(@".\private$\dhzOrderMgmt",false); 
			} 
			tbOut.Text +="==================\r\n"; 
			tbOut.Text +=mq.QueueName +":创建成功!\r\n"; 
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button4.Click += new System.EventHandler(this.Button4_Click);
			this.Button3.Click += new System.EventHandler(this.Button3_Click);
			this.Button2.Click += new System.EventHandler(this.Button2_Click);
			this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
			this.btnYibu.Click += new System.EventHandler(this.btnYibu_Click);
			this.btnSendToPub.Click += new System.EventHandler(this.btnSendToPub_Click);
			this.Button6.Click += new System.EventHandler(this.Button6_Click);
			this.Button7.Click += new System.EventHandler(this.Button7_Click);
			this.Button8.Click += new System.EventHandler(this.Button8_Click);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Error += new System.EventHandler(this.MSMQDemo_Error);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnCreate_Click(object sender, System.EventArgs e)
		{
			if(MessageQueue.Exists(@".\private$\dhzOrderMgmt")) 
			{ 
				mq=new MessageQueue(@".\private$\dhzOrderMgmt"); 
			} 
			else 
			{ 
				mq=MessageQueue.Create(@".\private$\dhzOrderMgmt",false); 
			} 
			tbOut.Text +="==================\r\n"; 
			tbOut.Text +=mq.QueueName +":创建成功!\r\n"; 
		}

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			Od od=new Od("代汉章","南昌",DateTime.Now.ToLongTimeString ()); 
			System.Messaging.Message myMessage = new System.Messaging.Message(od);
			mq.Send(myMessage); 
			tbOut.Text +="发送成功!\r\n"; 
		}

		private void btnRead_Click(object sender, System.EventArgs e)
		{
			if(mq!=null) 
			{ 
				mq.Formatter=new XmlMessageFormatter (new Type[]{typeof(Od)});
				System.Messaging.MessageEnumerator msg=mq.GetMessageEnumerator (); 
				while(msg.MoveNext ()) 
				{ 
					System.Messaging .Message oc=msg.Current ; 					 
					Od od=(Od)oc.Body ; 
					tbOut.Text +=od.Name +":"+od.City +":"+od.Time +";\r\n"; 
				} 
			} 
		}

		private void btnYibu_Click(object sender, System.EventArgs e)
		{
			msq=new MessageQueue(@".\private$\dhzTrans"); 
			if(msq.Transactional) 
			{ 
				MessageQueueTransaction myTransaction = new MessageQueueTransaction(); 
				msq.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted); 
				msq.Formatter=new System.Messaging.XmlMessageFormatter (new Type[]{Type.GetType("ASPNETMSMQ.Od" )}); 
				myTransaction.Begin(); 
				msq.BeginReceive(); 
				signal.WaitOne (); 
				myTransaction.Commit (); 
			} 
		}
		private void MyReceiveCompleted(Object source,ReceiveCompletedEventArgs asyncResult) 
		{ 
			try 
			{ 
				MessageQueue mq = (MessageQueue)source; 
				System.Messaging .Message m = mq.EndReceive(asyncResult.AsyncResult); 
				signal.Set (); 
				Od od=m.Body as Od ; 
				tbOut.Text+="Read:"+od.Name +":"+od.City +":"+od.Time +"\r\n"+"插入数据库完成\r\n"; 
				mq.BeginReceive(); 
			} 
			catch(MessageQueueException c) 
			{ 
				tbOut.Text+=("Error:"+c.Message +"\r\n"); 
				
			} 
			// Handle other exceptions. 


		} 
		private void btnSendToPub_Click(object sender, System.EventArgs e)
		{
			if(pubMsq==null) 
				pubMsq=new MessageQueue(qryname ); 
			if(pubMsq.Transactional ) 
			{ 
				Od od=new Od("万卿","景德镇",DateTime.Now.ToString("u")); 
				MessageQueueTransaction myTransaction = new 
					MessageQueueTransaction(); 
				myTransaction.Begin(); 
				pubMsq.Send(od,myTransaction); 
				myTransaction.Commit(); 
			} 
		}

		private void Button6_Click(object sender, System.EventArgs e)
		{
			pubMsq=new MessageQueue(qryname ); 
			if(pubMsq.Transactional) 
			{ 
				MessageQueueTransaction myTransaction = new MessageQueueTransaction(); 
				pubMsq.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted); 
				pubMsq.Formatter=new System.Messaging.XmlMessageFormatter (new Type[]{Type.GetType("Od" )}); 
				myTransaction.Begin(); 
				pubMsq.BeginReceive(); 
				signal.WaitOne (); 
				myTransaction.Commit (); 
			} 
		}

		private void Button7_Click(object sender, System.EventArgs e)
		{
			//删除创建的公共队列 
			System.Messaging.MessageQueue.Delete (qryname); 
		}

		private void Button8_Click(object sender, System.EventArgs e)
		{
			MessageQueue[] mq=System.Messaging.MessageQueue.GetPublicQueuesByMachine ("shaozhd"); 
			for(int i=0;i<mq.Length ;i++) 
			{ 
				tbOut.Text+=(mq[i].QueueName +":\r\n"); 
			} 
		}

		private void Button3_Click(object sender, System.EventArgs e)
		{
			tbOut.Text = "";
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			msq=new MessageQueue(@".\private$\dhzTrans"); 
			if(msq.Transactional) 
			{ 
				Od od=new Od("万卿","景德镇",DateTime.Now.ToString("u")); 
				System.Messaging.Message myMessage = new System.Messaging.Message(od);
				MessageQueueTransaction myTransaction = new MessageQueueTransaction(); 
				myTransaction.Begin(); 
				msq.Send(myMessage,myTransaction); 
				myTransaction.Commit(); 
				tbOut.Text+=("OK!发送成功:"+od.Time+"\r\n" ); 
			} 
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			msq=new MessageQueue(@".\private$\dhzTrans"); 
			if(msq .Transactional ) 
			{ 
				MessageQueueTransaction myTransaction = new MessageQueueTransaction(); 
				msq.Formatter=new System.Messaging.XmlMessageFormatter (new Type[]{typeof(Od)}); 
				myTransaction.Begin(); 
				System.Messaging.Message ms=msq.Receive(myTransaction); 
				Od od=ms.Body as Od ; 
				myTransaction.Commit(); 
				tbOut.Text +=od.Name +":"+od.City +":"+od.Time +";\r\n"; 
			} 
			
		}

		private void Button4_Click(object sender, System.EventArgs e)
		{
			if(MessageQueue.Exists(@".\private$\dhzTrans")) 
			{ 
				msq =new MessageQueue(@".\private$\dhzTrans"); 
			} 
			else 
			{ 
				msq=MessageQueue.Create(@".\private$\dhzTrans",true); 
			} 
			tbOut.Text+=("==================\r\n"); 
			tbOut.Text+=(msq.QueueName +":创建成功!\r\n"); 
		}

		private void MSMQDemo_Error(object sender, System.EventArgs e)
		{
			Server.ClearError();
		}

	
	}
	/////////////////////////================ 
	public class Od 
	{ 
		public Od() 
		{ 
			// 
			// TODO: 在此处添加构造函数逻辑 
			// 
		} 
		public Od(string na,string cty,string timestr) 
		{ 
			name=na; 
			city=cty; 
			time=timestr; 

		} 
		private string time=""; 
		private string name; 
		private string city; 
		public string Name 
		{ 
			get{return name ;} 
			set{name=value;} 
		} 
		public string City 
		{ 
			get{return city ;} 
			set{city=value;} 
		} 
		public string Time 
		{ 
			get{return time ;} 
			set{time=value;} 
		} 
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -