我需要在总线上进行异步消息传递。
这是我正在使用的代码:
//set callback to get the message
MessageReceiver messageReceiver = MessagingFactory.CreateMessageReceiver(BaseTopicName + "/subscriptions/" + addressee,
ReceiveMode.PeekLock);
IAsyncResult result = messageReceiver.BeginReceive(AsyncGet, messageReceiver);
Debug.WriteLine("After BeginReceive");
// Wait for the WaitHandle to become signaled.
Thread.Sleep(0);
result.AsyncWaitHandle.WaitOne();
// Close the wait handle.
result.AsyncWaitHandle.Close();
//return the information
Debug.WriteLine("return the information");
这是异步获取:
public void AsyncGet(IAsyncResult result)
{
Debug.WriteLine("Start AsyncGet");
MessageReceiver messageReceiver = result.AsyncState as MessageReceiver;
BrokeredMessage = messageReceiver.EndReceive(result);
Debug.WriteLine("Finish AsyncGet");
messageReceiver.Close();
}
我得到的输出是:
After BeginReceive
return the information
Start AsyncGet
Finish AsyncGet
它说那行 result.AsyncWaitHandle.WaitOne(); 正如我认为的那样,直到 AsyncGet 的线程完成后才停止执行。请告诉我我在这里做错了什么。谢谢!