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.Is Completed 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.BeginCon nect, 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
"Asynchrono us 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.EndConne ct method mainly calls
LazyAsyncResult .WaitForComplet ion(true), whose key code snippet is listed
below:
private object WaitForCompleti on(bool snap)
{
ManualResetEven t event1 = null;
bool flag1 = false;
if (!(snap ? this.IsComplete d : this.InternalPe ekCompleted))
{
event1 = (ManualResetEve nt) this.m_Event;
if (event1 == null)
{
flag1 = this.LazilyCrea teEvent(out event1);
}
}
.....
try
{
event1.WaitOne(-1, false);
......
}
Yes, this method checks "IsComplete d" property, if it is true,
Socket.EndConne ct 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.BeginCon nect method and will be signaled when the asynchronous
operation completes.
So Socket.EndConne ct method really checks "IsComplete d" property
internally, however, it will eliminate our loop checking on "IsComplete d"
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/subscripti...ult.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/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.