IAsyncResult question

D

David

I don't actually have a need to do this right now but was wondering... I
also assume the answer to this is going to be no but....

thread 1 calls an async socket.beginReceive() method which returns an
IAsyncResult. So at that moment thread 1 has this IAsyncResult. If the
callback method for that beginReceive calls beginReceive again, will thread
1's IAsyncResult reference from that first beginxxx call now reference the
second call, and so on? I assume no, it would produce a new IAsyncResult
object for each new beginxxx call.

To put it another way, I'm thinking from the perspective of that thread 1,
lets say it posts a beginReceive operation and it is concerned only with
when *all* the expected data is received, regardless of how many times the
receive callback iterates by repeating calls to beginReceive. The
IAsyncResult interface has the IsCompleted property and an AsyncWaitHandle.
So if that original IAsyncResult reference from thread 1 'followed' the
'whole operation from thread 1's perspective', it could be used to check on
completion status or wait from thread 1. Again, I assume this is not the
case and probably sounds real silly to those that know what their doing. I
assume the fact is that the IAsyncResult references only the one beginxxx
call it was created from, right?

I realize there are many other ways to accomplish what this example shows...
not looking to solve that, just wondering specifically about the
IAsyncResult with beginxxx() async calls.

any input is appreciated, laughing is perfectly acceptable as well,
considering I can't hear you ;)
 
N

Nicholas Paldino [.NET/C# MVP]

David,

While you are right, you can't take the result from the first async
operation (the IAsyncResult implementation) and use that to monitor the
larger operation (which involves multiple async operations), there is no
reason you can not abstract this out yourself.

For example, it would be very simple to have a boolean value somewhere
(with synchronized access, of course) and set that flag to true when the
overall operation is complete (which will be in one of your callbacks).
Then, thread 1 can just check the value when it needs to see if the
operation is complete.

Hope this helps.
 
D

David

Thanks Nicholas. Got it.

Nicholas Paldino said:
David,

While you are right, you can't take the result from the first async
operation (the IAsyncResult implementation) and use that to monitor the
larger operation (which involves multiple async operations), there is no
reason you can not abstract this out yourself.

For example, it would be very simple to have a boolean value somewhere
(with synchronized access, of course) and set that flag to true when the
overall operation is complete (which will be in one of your callbacks).
Then, thread 1 can just check the value when it needs to see if the
operation is complete.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

David said:
I don't actually have a need to do this right now but was wondering... I
also assume the answer to this is going to be no but....

thread 1 calls an async socket.beginReceive() method which returns an
IAsyncResult. So at that moment thread 1 has this IAsyncResult. If the
callback method for that beginReceive calls beginReceive again, will
thread 1's IAsyncResult reference from that first beginxxx call now
reference the second call, and so on? I assume no, it would produce a
new IAsyncResult object for each new beginxxx call.

To put it another way, I'm thinking from the perspective of that thread
1, lets say it posts a beginReceive operation and it is concerned only
with when *all* the expected data is received, regardless of how many
times the receive callback iterates by repeating calls to beginReceive.
The IAsyncResult interface has the IsCompleted property and an
AsyncWaitHandle. So if that original IAsyncResult reference from thread 1
'followed' the 'whole operation from thread 1's perspective', it could be
used to check on completion status or wait from thread 1. Again, I assume
this is not the case and probably sounds real silly to those that know
what their doing. I assume the fact is that the IAsyncResult references
only the one beginxxx call it was created from, right?

I realize there are many other ways to accomplish what this example
shows... not looking to solve that, just wondering specifically about the
IAsyncResult with beginxxx() async calls.

any input is appreciated, laughing is perfectly acceptable as well,
considering I can't hear you ;)
 

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