1
我需要在總線上進行異步消息傳遞。在ServiceBus上使用BeginReceive和EndReceive進行消息傳遞不適用於我
這是我使用的代碼:
//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");
這裏是AsyncGet:
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的線程完成後才停止執行,因爲我認爲它應該。 請告訴我我在這裏做錯了什麼。 謝謝!