Reputation: 979
In this example an asynchronous request is made and when it has completed, another asynchronous request is made from its callback. How could I be notified from the first thread (main) when the second callback is completed? Normally I would monitor the IAsyncResult, but the creation of the second request is not done in the scope of the first thread, so I don't have access to it.
Upvotes: 1
Views: 210
Reputation: 1985
The easiest way to get around this is probably not to do it - the second callback could be made syncronous (using GetResponse
instead of BeginGetResponse
), then you can just monitor the IAsyncResult.
Alternatively you could also use the same method they use to keep the main thread from ending: create another
private static ManualResetEvent allDone = new ManualResetEvent(false);
then use in the first callback
allDone.WaitOne();
and call
allDone.Set();
in the second callback
Upvotes: 1
Reputation: 171206
Your callback has to signal the first thread that it is done. It can do so using an event for example. TaskCompletionSource
is an await-friendly alternative.
Upvotes: 0