MSMQ(3)创建、同步异步接收消息

本文介绍了如何使用消息队列进行消息传递,包括创建队列、同步接收消息(接收后删除和保留)、以及异步接收消息(接收后删除和保留)。通过示例代码详细展示了不同接收方式的具体实现。
一、创建一个队列
None.gif   if  ( ! MessageQueue.Exists( " .\\Private$\\newPublicQueue " ))
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif           
// MessageQueue.Create(".\\newPublicQueue");//创建一个公共队列
InBlock.gif
            MessageQueue.Create(".\\Private$\\newPublicQueue");//创建一个私有队列
ExpandedBlockEnd.gif
        }
二、同步接收消息

1)接收消息,接受成功后删除
None.gif  MessageQueue queue  =   new  MessageQueue( " .\\Private$\\newPublicQueue " );
None.gif
None.gif        Message message 
=  queue.Receive(); //  Receive message, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到,接收之后就删除
ExpandedBlockStart.gifContractedBlock.gif
        message.Formatter  =   new  XmlMessageFormatter( new  Type[]  dot.gif typeof(string) } );
None.gif        
this .TextBox2.Text  =  message.Body.ToString();
2)接受消息,接收成功后保留
None.gif   MessageQueue queue  =   new  MessageQueue( " .\\Private$\\newPublicQueue " );
None.gif
None.gif        Message message 
=  queue.Peek(); //  异步接收消息。接收之后不删除
ExpandedBlockStart.gifContractedBlock.gif
        message.Formatter  =   new  XmlMessageFormatter( new  Type[]  dot.gif typeof(string) } );
None.gif        
this .TextBox4.Text  =  message.Body.ToString();
三、异步接收消息
1)接收消息,接受成功后删除
None.gif  MessageQueue queue  =   new  MessageQueue( " .\\Private$\\newPublicQueue " );
None.gif
None.gif        
//  queue.BeginReceive(); //  异步接收消息。接收之后就删除
None.gif        
//  给接收结束加一个委托
None.gif
        queue.ReceiveCompleted  +=
None.gif            
new  ReceiveCompletedEventHandler(MyReceiveCompleted);
None.gif
None.gif        
// 开始接收
None.gif
        queue.BeginReceive();
None.gif
private   static   void  MyReceiveCompleted(Object source, 
None.gif            ReceiveCompletedEventArgs asyncResult)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Connect to the queue.
InBlock.gif
                MessageQueue mq = (MessageQueue)source;
InBlock.gif
InBlock.gif                
// End the asynchronous receive operation.
InBlock.gif
                Message m = mq.EndReceive(asyncResult.AsyncResult);
ExpandedSubBlockStart.gifContractedSubBlock.gif                m.Formatter 
= new XmlMessageFormatter(new Type[] dot.giftypeof(string) });
InBlock.gif                System.IO.StreamWriter fs1 
= new System.IO.StreamWriter("e:\\11.txt"false, System.Text.Encoding.Default);// System.IO.File.CreateText("c:\\testAsyn.txt");
InBlock.gif
                fs1.WriteLine(m.Body.ToString());
InBlock.gif                fs1.Close();
InBlock.gif
InBlock.gif                count 
+= 1;
InBlock.gif                
if (count == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    signal.Set();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
// Restart the asynchronous receive operation.
InBlock.gif
                mq.BeginReceive();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(MessageQueueException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Handle sources of MessageQueueException.
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
InBlock.gif        
InBlock.gif            
InBlock.gif            
return
ExpandedBlockEnd.gif        }
2)接受消息,接收成功后保留
None.gif  MessageQueue queue1  =   new  MessageQueue( " .\\Private$\\newPublicQueue " );
None.gif
None.gif        
//  queue.BeginReceive(); //  异步接收消息。接收之后就删除
None.gif        
//  给接收结束加一个委托
None.gif
        queue1.PeekCompleted  +=
None.gif            
new  PeekCompletedEventHandler(PeekMyReceiveCompleted);
None.gif
None.gif        
// 开始接收
None.gif
        queue1.BeginPeek();
None.gif
private   static   void  PeekMyReceiveCompleted(Object source,
None.gif           PeekCompletedEventArgs asyncResult)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Connect to the queue.
InBlock.gif
            MessageQueue mq = (MessageQueue)source;
InBlock.gif
InBlock.gif            
// End the asynchronous receive operation.
InBlock.gif
            Message m = mq.EndPeek(asyncResult.AsyncResult);
ExpandedSubBlockStart.gifContractedSubBlock.gif            m.Formatter 
= new XmlMessageFormatter(new Type[] dot.giftypeof(string) });
InBlock.gif            System.IO.StreamWriter fs1 
= new System.IO.StreamWriter("e:\\22.txt"false, System.Text.Encoding.Default);// System.IO.File.CreateText("c:\\testAsyn.txt");
InBlock.gif
            fs1.WriteLine(m.Body.ToString());
InBlock.gif            fs1.Close();
InBlock.gif
InBlock.gif            count 
+= 1;
InBlock.gif            
if (count == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                signal.Set();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Restart the asynchronous receive operation.
InBlock.gif
            mq.BeginPeek();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (MessageQueueException)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Handle sources of MessageQueueException.
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
return;
ExpandedBlockEnd.gif    }
完整代码下载

转载于:https://www.cnblogs.com/gjahead/archive/2007/08/05/843934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值