fordeka
fordeka

Reputation: 979

How can you be notified if an AsyncCallback to an asynchronous request has been completed?

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

Answers (2)

Ken
Ken

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

usr
usr

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

Related Questions