C#中使用Windows消息队列服务(MSMQ)简单示例

using  System;
InBlock.gif using  System.Collections.Generic;
InBlock.gif using  System.ComponentModel;
InBlock.gif using  System.Data;
InBlock.gif using  System.Drawing;
InBlock.gif using  System.Text;
InBlock.gif using  System.Windows.Forms;
InBlock.gif using  System.Messaging;
InBlock.gif
namespace  WindowsApplication1
InBlock.gif {
InBlock.gif          public  partial  class  Form1 : Form
InBlock.gif         {
InBlock.gif                  public  Form1()
InBlock.gif                 {
InBlock.gif                         InitializeComponent();
InBlock.gif                 }
InBlock.gif
                 string  QueuePath =  ".\\private$\\test" ;
InBlock.gif                 IMessageFormatter formatter =  new  System.Messaging.BinaryMessageFormatter();
InBlock.gif
                 private   void  button1_Click( object  sender, EventArgs e)
InBlock.gif                 {
InBlock.gif                         CreateMessageQueue(QueuePath);
InBlock.gif                         SendMessage(QueuePath, CreateMessage(richTextBox1.Text, formatter));
InBlock.gif                 }
InBlock.gif
                 private   void  button2_Click( object  sender, EventArgs e)
InBlock.gif                 {
InBlock.gif                         System.Messaging.Message msg = ReceiveMessage(QueuePath);
InBlock.gif                         msg.Formatter = formatter;
InBlock.gif                         richTextBox2.Text = msg.Body.ToString();
InBlock.gif                 }
InBlock.gif
                 private  System.Messaging.Message CreateMessage( string  text, IMessageFormatter formatter)
InBlock.gif                 {
InBlock.gif                         System.Messaging.Message message =  new  System.Messaging.Message();
InBlock.gif                         message.Body = text;
InBlock.gif                         message.Formatter = formatter;
InBlock.gif                          return  message;
InBlock.gif                 }
InBlock.gif
                 private   void  CreateMessageQueue( string  queuePath)
InBlock.gif                 {
InBlock.gif                          if  (!MessageQueue.Exists(queuePath))
InBlock.gif                         {
InBlock.gif                                 MessageQueue queue = MessageQueue.Create(queuePath);
InBlock.gif                                 queue.SetPermissions( "Administrators" , MessageQueueAccessRights.FullControl);
InBlock.gif                                 queue.Label = queuePath;
InBlock.gif                         }
InBlock.gif                 }
InBlock.gif
                 private   bool  SendMessage( string  queuePath, System.Messaging.Message msg)
InBlock.gif                 {
InBlock.gif                          if  (!MessageQueue.Exists(queuePath))
InBlock.gif                         {
InBlock.gif                                  return   false ;
InBlock.gif                         }
InBlock.gif
                        MessageQueue queue =  new  System.Messaging.MessageQueue(queuePath);
InBlock.gif                         queue.Send(msg);
InBlock.gif                          return   true ;
InBlock.gif                 }
InBlock.gif
                 private  System.Messaging.Message ReceiveMessage( string  queuePath)
InBlock.gif                 {
InBlock.gif                          if  (!MessageQueue.Exists(queuePath))
InBlock.gif                         {
InBlock.gif                                  return   null ;
InBlock.gif                         }
InBlock.gif
                        MessageQueue queue =  new  MessageQueue(queuePath);
InBlock.gif                         System.Messaging.Message message = queue.Receive();
InBlock.gif                          return  message;
InBlock.gif                 }
InBlock.gif         }
InBlock.gif }










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/184323,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值