I have a third party service which have async DoAsync() operation and Done() event. How can I create my own sync DoSync() operation? I want smth like this (in pseudocode):
operation DoSync()
{
DoAsync();
wait until Done();
}
I have a third party service which have async DoAsync() operation and Done() event. How can I create my own sync DoSync() operation? I want smth like this (in pseudocode):
operation DoSync()
{
DoAsync();
wait until Done();
}
One way to do this is to temporarily add an event handler, and in that handler, set some sort of waitable object. Here's an example that shows the technique with one of the async methods provided by
WebClient
Here's a simplified version that makes the basic technique easier to see, but which doesn't bother with things like error handling, or tidying up after itself:
In most situations you will want to make sure you detect errors, and detach the handler when you're done - I just provided the shorter version to help illustrate the point. I wouldn't actually use that simplified one in a real program.
Try the AutoResetEvent http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx