C#之消息队列的简要说明----自学笔记[5]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 dhz123 的 blog

    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 ();
   }
  
  }

  

 }

本文关键:C#之消息队列的简要说明----自学笔记
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top