惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - 过江

多线程业务实现疑问 委托的三种写法 LinqToXml学习实例 String.format使用( 转) - 过江 - 博客园 TFS 安装手册以及常用问题解决方法 SQL server 系统优化--通过执行计划优化索引(1) (转) .Net新建、卸载、调试Windows服务 请问在用C#+Mapx开始,怎样根据图元名称获得该图元已经选中的图元 C#基础:ref和out的区别 仿163邮箱的alert提示,beta1.1 (转) 好久都没有写东西了 终于用上CodeSmith4.0了,跟大家一起分享 XSLT基本语法和第一个实例 扩展TreeView控件(1) - 联动复选框(复选框的全选和取消全选)(转) 动态地生成用户输入的函数表达式(C#) (转) 画函数图形的C#程序(改进版) (转) 画函数图形的C#程序,兼论一个病态函数 (转) 获取M$ SQL Server用户表的字段信息 (转) 关于Remoting服务启动和停止的简单总结 (转)
MSMQ(3)创建、同步异步接收消息
过江 · 2007-08-05 · via 博客园 - 过江

一、创建一个队列

 if (!MessageQueue.Exists(".\\Private$\\newPublicQueue"))
        
{
           
// MessageQueue.Create(".\\newPublicQueue");//创建一个公共队列
            MessageQueue.Create(".\\Private$\\newPublicQueue");//创建一个私有队列
        }

二、同步接收消息

1)接收消息,接受成功后删除

 MessageQueue queue = new MessageQueue(".\\Private$\\newPublicQueue");

        Message message 
= queue.Receive();// Receive message, 同步的Receive方法阻塞当前执行线程,直到一个message可以得到,接收之后就删除
        message.Formatter = new XmlMessageFormatter(new Type[] typeof(string) });
        
this.TextBox2.Text = message.Body.ToString();

2)接受消息,接收成功后保留

  MessageQueue queue = new MessageQueue(".\\Private$\\newPublicQueue");

        Message message 
= queue.Peek();// 异步接收消息。接收之后不删除
        message.Formatter = new XmlMessageFormatter(new Type[] typeof(string) });
        
this.TextBox4.Text = message.Body.ToString();

三、异步接收消息
1)接收消息,接受成功后删除

 MessageQueue queue = new MessageQueue(".\\Private$\\newPublicQueue");

        
// queue.BeginReceive();// 异步接收消息。接收之后就删除
        
// 给接收结束加一个委托
        queue.ReceiveCompleted +=
            
new ReceiveCompletedEventHandler(MyReceiveCompleted);

        
//开始接收
        queue.BeginReceive();
private static void MyReceiveCompleted(Object source, 
            ReceiveCompletedEventArgs asyncResult)
        
{
            
try
            
{
                
// Connect to the queue.
                MessageQueue mq = (MessageQueue)source;

                
// End the asynchronous receive operation.
                Message m = mq.EndReceive(asyncResult.AsyncResult);
                m.Formatter 
= new XmlMessageFormatter(new Type[] typeof(string) });
                System.IO.StreamWriter fs1 
= new System.IO.StreamWriter("e:\\11.txt"false, System.Text.Encoding.Default);// System.IO.File.CreateText("c:\\testAsyn.txt");
                fs1.WriteLine(m.Body.ToString());
                fs1.Close();

                count 
+= 1;
                
if (count == 2)
                
{
                    signal.Set();
                }


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

            
catch(MessageQueueException)
            
{
                
// Handle sources of MessageQueueException.
            }

            
        
            
            
return
        }

2)接受消息,接收成功后保留

 MessageQueue queue1 = new MessageQueue(".\\Private$\\newPublicQueue");

        
// queue.BeginReceive();// 异步接收消息。接收之后就删除
        
// 给接收结束加一个委托
        queue1.PeekCompleted +=
            
new PeekCompletedEventHandler(PeekMyReceiveCompleted);

        
//开始接收
        queue1.BeginPeek();
private static void PeekMyReceiveCompleted(Object source,
           PeekCompletedEventArgs asyncResult)
    
{
        
try
        
{
            
// Connect to the queue.
            MessageQueue mq = (MessageQueue)source;

            
// End the asynchronous receive operation.
            Message m = mq.EndPeek(asyncResult.AsyncResult);
            m.Formatter 
= new XmlMessageFormatter(new Type[] typeof(string) });
            System.IO.StreamWriter fs1 
= new System.IO.StreamWriter("e:\\22.txt"false, System.Text.Encoding.Default);// System.IO.File.CreateText("c:\\testAsyn.txt");
            fs1.WriteLine(m.Body.ToString());
            fs1.Close();

            count 
+= 1;
            
if (count == 2)
            
{
                signal.Set();
            }


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

        
catch (MessageQueueException)
        
{
            
// Handle sources of MessageQueueException.
        }




        
return;
    }

完整代码下载