How can you block until the completion of the asynchronous event?
Here is a way to block until an event is raised by setting a flag in the event handler and polling the flag:
private object DoAsynchronousCallSynchronously()
{
int completed = 0;
AsynchronousObject obj = new AsynchronousObject();
obj.OnCompletedCallback += delegate { Interlocked.Increment(ref completed); };
obj.StartWork();
while (completed == 0)
Thread.Sleep(50);
return obj.Result;
}
Is there any way to do this without polling?
Jim
source
share