I need asynchronous messaging on the bus.
This is the code I'm using:
//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");
Here is the 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();
}
The output I get is:
After BeginReceive
return the information
Start AsyncGet
Finish AsyncGet
It says that the line result.AsyncWaitHandle.WaitOne(); did not stop execution until the thread of AsyncGet finishes, as I thought it should. Please,tell me what I'm doing wrong here. Thanks!