【发布时间】:2013-04-30 22:21:35
【问题描述】:
我目前正在从我的 MSMQ 中读取数据(为简洁起见):
public void Start()
{
this.queue.ReceiveCompleted += this.ReceiveCompleted;
this.queue.BeginReceive();
}
void ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
this.queue.EndReceive(e.AsyncResult);
try
{
var m = e.Message;
m.Formatter = this.formatter;
this.Handle(m.Body);
}
finally
{
this.queue.BeginReceive();
}
}
但是,这只允许我串行处理消息。如何修改此代码以允许并行消息处理?
我知道我可以将this.queue.BeginReceive(); 移出finally 并移到ReceiveCompleted 的顶部,但是如何阻止产生与我有消息一样多的线程?如何明智地控制并行度,以免线程池泛滥?是否有一些内置机制,或者我必须编写自己的经理?
编辑:我的目标是更快地处理消息。消息的处理涉及对第 3 方的异步调用,因此目前我的实现正在浪费大量时间来通过队列。
谢谢
【问题讨论】: