Asynchronous methods: EndInvoke versus Waiting on Handle

T

Taras_96

Hi everyone,

The page at http://en.csharp-online.net/CSharp_Delegates_and_Events—Asynchronous_method_calls

states that EndInvoke will block execution on the current thread until
the asynchronous method has finished executing.

Another method described is waiting on a signal:

"The next technique is to block on the IAsyncResult object’s wait
handle. When the asynchronous method is complete, the IAsyncResult
object’s AsyncWaitHandle will be signaled. Then any thread waiting on
that handle will be awakened. Once awake, the thread can call the
IAsyncResult object’s EndInvoke method to receive the return value."

Is there any difference in the two methods? When would you use one or
the other. If anything the second option seems more complicated as it
requires an extra call in order to get the result.

Thanks

Taras
 
B

Brian Rasmussen [C# MVP]

Generally you should avoid blocking if possible. The optimal use of
asynchronous methods is to provide a callback which will execute once the
async operation is complete. This means no blocking on the calling thread.
Unfortunately the programming model is a little more complex. Jeffrey
Richter has a series of articles in MSDN magazine on how to make this model
a little more elegant.

Regards,
Brian Rasmussen [C# MVP]

Hi everyone,

The page at
http://en.csharp-online.net/CSharp_Delegates_and_Events—Asynchronous_method_calls

states that EndInvoke will block execution on the current thread until
the asynchronous method has finished executing.

Another method described is waiting on a signal:

"The next technique is to block on the IAsyncResult object’s wait
handle. When the asynchronous method is complete, the IAsyncResult
object’s AsyncWaitHandle will be signaled. Then any thread waiting on
that handle will be awakened. Once awake, the thread can call the
IAsyncResult object’s EndInvoke method to receive the return value."

Is there any difference in the two methods? When would you use one or
the other. If anything the second option seems more complicated as it
requires an extra call in order to get the result.

Thanks

Taras
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top