RE: can I use IAsyncResult.IsCompleted Property to know if Endxxx was already called?



Hi Semedao,

Based on my understanding, your main thread is performing some asynchronous
socket operations with Socket.BeginXXX methods. After calling
Socket.BeginXXX method in this main thread , you want to know if you should
check the IAsyncResult.IsCompleted property to determine the operation is
complete so that there is no need to call Endxxx method in the main thread.
If I have misunderstood your problem context, please feel free to tell me,
thanks.

Normally, in the Socket Asynchronous programming model, before you call
Socket.BeginConnect, you can create acallback method that implements the
AsyncCallback delegate and pass its name to the BeginConnect method as
second parameter. This callback method will execute in a separate thread
and is called by the system after BeginConnect returns. The callback method
must accept the IAsyncResult returned by the BeginConnect method as a
parameter. And after obtaining the Socket in the callback method, you can
call the EndConnect method to successfully complete the connection attempt.
Note: the EndConnect is executed in the threadpool thread and will block
until connected.

So there is no need for your main thread to call EndConnect method, but it
is the callback method executed in the threadpool thread that should call
EndConnect method.

I think your main concern may be that: in the main thread, after calling
Socket.BeginXXX method, what should you do to know that the operation is
finished. Since the callback thread knows the operation status, normally,
you may use an event to synchronize and communicate between your main
thread and the callback executing thread. The sample code in the article
below demonstrates the logic
"Asynchronous Client Socket Example"
http://msdn2.microsoft.com/en-us/library/bew39x2a.aspx

Finally, if you want to know the internal work, by using Reflector, you
will see that, Socket.EndConnect method mainly calls
LazyAsyncResult.WaitForCompletion(true), whose key code snippet is listed
below:

private object WaitForCompletion(bool snap)
{
ManualResetEvent event1 = null;
bool flag1 = false;
if (!(snap ? this.IsCompleted : this.InternalPeekCompleted))
{
event1 = (ManualResetEvent) this.m_Event;
if (event1 == null)
{
flag1 = this.LazilyCreateEvent(out event1);
}
}
.....
try
{
event1.WaitOne(-1, false);
......
}

Yes, this method checks "IsCompleted" property, if it is true,
Socket.EndConnect method will return without sleeping. If not, it means the
connect operation is still pending, it will obtain the internal "m_Event"
and wait on this event to signal. Note: "m_Event" is created in the
Socket.BeginConnect method and will be signaled when the asynchronous
operation completes.

So Socket.EndConnect method really checks "IsCompleted" property
internally, however, it will eliminate our loop checking on "IsCompleted"
property, but wait on an internal event to get notification.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

.



Relevant Pages

  • RE: Missing error in WSAAsyncSelect event completion in x64 enviro
    ... SOCKET Listen; ... // Receive data from the socket in wParam ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.win32.programmer.networks)
  • Re: Select between raw socket programming vs. WCF
    ... Does WCF support compression? ... Use raw socket and deine your own communication protocol to transfer ... For WCF, it does support XML webservice conform interface/endpoint, ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.webservices)
  • RE: looking for samples
    ... A reusable, high performance, socket server class - Part 1 ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ... project analysis and dump analysis issues. ...
    (microsoft.public.inetserver.asp.components)
  • Re: Simple TCP Socket Communicator - Please Help
    ... just like what you can do in unmanaged socket application, ... Microsoft MSDN Online Support Lead ... TcpClient to receive data from server, ... private StreamReader _reader; ...
    (microsoft.public.dotnet.framework)
  • Re: XP Home rebooting randomly
    ... off that is being initiated by the power supply electronics or the CPU ... Dust is a heat insulator, ... Some BIOS chips support temperature and some also support fan speed ... it in a socket, you don't want to flex an electrically live motherboard into ...
    (microsoft.public.windowsxp.help_and_support)