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();
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
WebClientHere’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.