473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

In asynchronous model, IAsyncResult.AsyncWaitHandle signaled first, or AsyncCallback invoked first?

In asynchronous model, BeginXXX method returns a IAsnycResult-derived
object. IAsyncResult.AsyncWaitHandle will be signaled when the
asynchronous job is complete; and BeginXXX method has a argument as
AsyncCallback to be invoked when the job is complete.

I am wondering. When the asynchronous is complete, is
IAsyncResult.AsyncWaitHandle be signaled first, or AsyncCallback be
invoked first?

Aug 2 '07 #1
2 2182
Morgan Cheng wrote:
[...]
I am wondering. When the asynchronous is complete, is
IAsyncResult.AsyncWaitHandle be signaled first, or AsyncCallback be
invoked first?
Surely they are effectively simultaneous. That is, obviously one
happens before the other. But they likely occur in practically
immediate sequence, and because of the way thread scheduling works on
Windows, it's entirely possible that if you have both a callback and a
thread waiting on the WaitHandle, that either the code in the callback
or the code waiting on the WaitHandle could get scheduled next.

In other words, even if one or the other is technically always done
first, the observed result could in fact be the opposite. I would
suggest that this is one of those things that you should not assume is
well-defined and should not rely on a specific behavior in code,
especially if you are asking as a general question related to anything
that uses IAsyncResult (even if the behavior is well-defined for a
particular class, I'm not sure you could depend on it being well-defined
for _all_ classes that implement the asynchronous paradigm).

I'm curious if there's a specific scenario in which this question came
up? It seems like an interesting enough academic question, but since
you would normally use _either_ the WaitHandle _or_ a callback, not both
together, it's not clear why this would come up in actual code.

Pete
Aug 2 '07 #2
On Aug 2, 2:34 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
Morgan Cheng wrote:
[...]
I am wondering. When the asynchronous is complete, is
IAsyncResult.AsyncWaitHandle be signaled first, or AsyncCallback be
invoked first?

Surely they are effectively simultaneous. That is, obviously one
happens before the other. But they likely occur in practically
immediate sequence, and because of the way thread scheduling works on
Windows, it's entirely possible that if you have both a callback and a
thread waiting on the WaitHandle, that either the code in the callback
or the code waiting on the WaitHandle could get scheduled next.

In other words, even if one or the other is technically always done
first, the observed result could in fact be the opposite. I would
suggest that this is one of those things that you should not assume is
well-defined and should not rely on a specific behavior in code,
especially if you are asking as a general question related to anything
that uses IAsyncResult (even if the behavior is well-defined for a
particular class, I'm not sure you could depend on it being well-defined
for _all_ classes that implement the asynchronous paradigm).

I'm curious if there's a specific scenario in which this question came
up? It seems like an interesting enough academic question, but since
you would normally use _either_ the WaitHandle _or_ a callback, not both
together, it's not clear why this would come up in actual code.

Pete

I did some googling and find this post
http://www.bluebytesoftware.com/blog...tAddendum.aspx.
The correct sequence should be
1) set IsCompleted to true,
2) signal the handle,
3) invoke the callback.

This is reasonable, because the callback function might wait on the
AsyncWaitHandle. If the sequence of 2) & 3) is inverted, it would be a
deadlock.
Even the callback doesn't depends on the AsyncWaitHandle, it might be
a time-costing operation, which can delay signaling of
AsyncWaitHandle. And, if other operations depends on AsyncWaitHandle,
they are delayed. That's not good.

With the help of Lutz Roeder's .Net Reflector, I read the reflected
code of System.Web.Services.Protocols.WebClientAsyncResult , which is a
subclass of IAsyncResult. Its Complete function does in the sequence
as mentioned above.

Of course the implementation can be like this
1) set IsCompleted to true;
2) invoke the callback with BeginInvoke;
3) signal the handle
But this involves a unnecessary threading switch.





Aug 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael C | last post by:
Hi all, Is there an easy way to get the parameters of an asynchronous delegate call from the callback function? Here's an example of what I'm trying to do: private delegate ArrayList...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
5
by: Ryan Liu | last post by:
Hi, I read Microsoft SDK, ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming overview.htm there are 4 ways to call EndInvoke: The code in this topic demonstrates...
9
by: darthghandi | last post by:
I am trying to create a server application using asynchronous sockets. I run into a problem when I try to connect to my server using a non-.net program. I can establish the connection, and send...
2
by: David | last post by:
If I were to call an async beginxxx method that returns an IAsyncResult when exactly would the AsyncWaitHandle be signaled? Before or after the corresponding endxxx method completes? Immediately...
0
by: APA | last post by:
I've seen the MS sample async web request pattern and I ask is it really async if it is using a ManualResetEvent and setting WaitOne()? The ManualResetEvent object is being declared as a static...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.