473,811 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can I use IAsyncResult.Is Completed Property to know if Endxxx was already called?

Hi all,
someone know if I can use the IAsyncResult.Is Completed Property of
IAsyncResult that return from Socket.Beginxxx methods to determine if the
Endxxx method already called perior or not ?
I have methods that return the IAsyncResult after I called begin... (connect
for example)
and I want to know if I should call the EndConnect or not outside the
method.
Thanks
Oct 25 '06 #1
12 4639
Hi,

You should always call EndConnect regardless.

--
Dave Sexton

"semedao" <se*****@commun ity.nospamwrote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
Hi all,
someone know if I can use the IAsyncResult.Is Completed Property of
IAsyncResult that return from Socket.Beginxxx methods to determine if the
Endxxx method already called perior or not ?
I have methods that return the IAsyncResult after I called begin... (connect
for example)
and I want to know if I should call the EndConnect or not outside the
method.
Thanks

Oct 25 '06 #2
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.

Oct 26 '06 #3
Hi ,
I know , but the question is how to determine if I already called Endconnect
for this socket in other place
instead of writing some flags , I want to use something in the
IasyncResult..
because calling EndConnect second time will throw exception.
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
Hi,

You should always call EndConnect regardless.

--
Dave Sexton

"semedao" <se*****@commun ity.nospamwrote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
>Hi all,
someone know if I can use the IAsyncResult.Is Completed Property of
IAsyncResult that return from Socket.Beginxxx methods to determine if the
Endxxx method already called perior or not ?
I have methods that return the IAsyncResult after I called begin...
(connect for example)
and I want to know if I should call the EndConnect or not outside the
method.
Thanks


Oct 27 '06 #4
Hi Semedao,

Have you read Jeffrey's article?

--
Dave Sexton

"semedao" <se*****@commun ity.nospamwrote in message
news:eg******** ********@TK2MSF TNGP02.phx.gbl. ..
Hi ,
I know , but the question is how to determine if I already called Endconnect
for this socket in other place
instead of writing some flags , I want to use something in the
IasyncResult..
because calling EndConnect second time will throw exception.
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:%2******** **********@TK2M SFTNGP05.phx.gb l...
>Hi,

You should always call EndConnect regardless.

--
Dave Sexton

"semedao" <se*****@commun ity.nospamwrote in message
news:%2******* ***********@TK2 MSFTNGP05.phx.g bl...
>>Hi all,
someone know if I can use the IAsyncResult.Is Completed Property of
IAsyncResul t that return from Socket.Beginxxx methods to determine if the
Endxxx method already called perior or not ?
I have methods that return the IAsyncResult after I called begin...
(connect for example)
and I want to know if I should call the EndConnect or not outside the
method.
Thanks



Oct 27 '06 #5
Hi Semedao,

How about this issue now? Is it resolved? If you still need any help or
have any concern, please feel free to feedback, thanks.

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.

Nov 1 '06 #6
Hi Jeffrey
first , tanks for your answer
I think I need to explain my "big" problem...
I try to make a connection between 2 peers behind 2 Nats
what called: "hole punching"
based on the article in:
http://www.brynosaurus.com/pub/net/p2pnat/
we need to start async connect calls from both peers to each other.
It was already work for me in some routers test
until I came to router that does not return me "refused" exception imidiatly
on this type of router that does not return nothing.. the peer that made the
"beginConne ct" will wait for (in my comp) 20-21 seconds , until he can make
the next beginconnect
this will break the all hole punching... , because I need to send "syn_sent"
to the other peer every short period (like 1 second)
this was the reason to try playing with the asyncresult
If I start new socket before endconnect complete and make beginconnect it
will throw exception of
"SocketExceptio n : System.Net.Sock ets.SocketExcep tion: Only one usage of
each socket address (protocol/network address/port) is normally permitted"
It happen also when I use the
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);

or

_socket.Exclusi veAddressUse = false;

I try to make

ar.AsyncWaitHan dle.WaitOne(100 0,false);

In the calling thread of the BeginConnect after the call , and :

_socket.Shutdow n(SocketShutdow n.Both);

_socket.Close() ;

or

_socket.Disconn ect(false);

before create the new Socket for the next beginConnect

also I try to set the

_socket.SendTim eout = 1000;

_socket.Receive Timeout = 1000;

values , thinking thatit will raise to me the SocketException of timeout
after only 1 second...

nothing work...

so , I am stack here with this problem...

If I had some option to call the next beginConnect that even if fail - will
success to send the "syn_sent" to the remote peer in some way - it can solve
me the problem

Thanks

""Jeffrey Tan[MSFT]"" <je***@online.m icrosoft.comwro te in message
news:QT******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hi Semedao,

How about this issue now? Is it resolved? If you still need any help or
have any concern, please feel free to feedback, thanks.

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.

Nov 6 '06 #7
Hi,
I just discussed with Jeffery and we think that we have not totally
understood your issue. Te let us better understand your issue, could you
please answer me some questions?
1. What did you mean that you could call next BeginConnect after 20s?
2. Did all BeginConnect methods connect to the same endpoint?
3. How did you send the "syn_sent" to the endpoint if the connection had
not been established?
4. Why did you want to break the connection after 1s since it had not been
established? Wouldn't it also fail when the next BeginConnect was invoked?
5. Could you please mail me (ch******@micro soft.com) a sample project for
further research?

Sincerely yours,
Charles Wang
Microsoft Online Community Support

=============== =============== =============== =========
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from this issue.
=============== =============== =============== =========
This posting is provided "AS IS" with no warranties, and confers no rights.
=============== =============== =============== =========
Nov 7 '06 #8
Hi Semedao,
Just check to see if you are still paying attention on this issue. We are
interested in this issue. If you could post back and let us know this issue
more detailed, we will be very glad and encouraged to assist further.

Though this issue is hard to troubleshoot due to its complexity and only
some routers leading to this problem, your detailed information will prompt
our understanding and research on this issue so that we can piont out a
direction to you. If we found that this issue is really hard to be resolved
at our newsgroup, we will escalate you to a senior team for best support.

If you have any other questions or concerns, please feel free to let me
know. I will be more that happy to be of assistance.

Sincerely yours,
Charles Wang
Microsoft Online Community Support

Nov 9 '06 #9
Hi,

Nice article, thanks for the link.
ar.AsyncWaitHan dle.WaitOne(100 0,false);

In the calling thread of the BeginConnect after the call , and :

_socket.Shutdow n(SocketShutdow n.Both);

_socket.Close() ;

or

_socket.Disconn ect(false);

before create the new Socket for the next beginConnect
What is preventing you from using this approach? Is there an exception being
thrown? What is it?

The following code should work:

IAsyncResult ar = socket.BeginCon nect(...);

if (!ar.AsyncWaitH andle.WaitOne(1 000, false))
socket.Close();

Realize that the connection may have been established if you waited longer,
but the timeout will expire after one second regardless (even if this isn't
true in the particular case you are testing it could be true in others). Due
to network latency and its indeterministic nature, it's possible that
subsequent attempts will fail the same, although given enough time would
complete successfully. The timeout of one second might be too restrictive
(although I'd imagine not in most circumstances now that dial-up is becoming
less and less commonplace in home networks; I hope :). I don't believe that
the timeout of one second is required for "hole punching" either. One second
is simply used as an example in the article [§4.2 Opening Peer-to-Peer TCP
Streams].

You may want to allow the connection to timeout. If it's not working still
then it's possible that the NAT isn't behaving according to hole-punching
requirements.
also I try to set the

_socket.SendTim eout = 1000;

_socket.Receive Timeout = 1000;
These timeouts only apply to the Send and Receive methods (not the Begin*
methods). And I don't believe there is a timeout setting available for the
Connect method (or BeginConnect).

--
Dave Sexton

"semedao" <se*****@commun ity.nospamwrote in message
news:uM******** ******@TK2MSFTN GP03.phx.gbl...
Hi Jeffrey
first , tanks for your answer
I think I need to explain my "big" problem...
I try to make a connection between 2 peers behind 2 Nats
what called: "hole punching"
based on the article in:
http://www.brynosaurus.com/pub/net/p2pnat/
we need to start async connect calls from both peers to each other.
It was already work for me in some routers test
until I came to router that does not return me "refused" exception imidiatly
on this type of router that does not return nothing.. the peer that made the
"beginConne ct" will wait for (in my comp) 20-21 seconds , until he can make
the next beginconnect
this will break the all hole punching... , because I need to send "syn_sent"
to the other peer every short period (like 1 second)
this was the reason to try playing with the asyncresult
If I start new socket before endconnect complete and make beginconnect it
will throw exception of
"SocketExceptio n : System.Net.Sock ets.SocketExcep tion: Only one usage of
each socket address (protocol/network address/port) is normally permitted"
It happen also when I use the
_socket.SetSock etOption(Socket OptionLevel.Soc ket,
SocketOptionNam e.ReuseAddress, 1);

or

_socket.Exclusi veAddressUse = false;

I try to make

ar.AsyncWaitHan dle.WaitOne(100 0,false);

In the calling thread of the BeginConnect after the call , and :

_socket.Shutdow n(SocketShutdow n.Both);

_socket.Close() ;

or

_socket.Disconn ect(false);

before create the new Socket for the next beginConnect

also I try to set the

_socket.SendTim eout = 1000;

_socket.Receive Timeout = 1000;

values , thinking thatit will raise to me the SocketException of timeout
after only 1 second...

nothing work...

so , I am stack here with this problem...

If I had some option to call the next beginConnect that even if fail - will
success to send the "syn_sent" to the remote peer in some way - it can solve
me the problem

Thanks

""Jeffrey Tan[MSFT]"" <je***@online.m icrosoft.comwro te in message
news:QT******** ******@TK2MSFTN GXA01.phx.gbl.. .
>Hi Semedao,

How about this issue now? Is it resolved? If you still need any help or
have any concern, please feel free to feedback, thanks.

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.


Nov 10 '06 #10

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

Similar topics

2
1763
by: Edward Diener | last post by:
How does one specify in a component that a property is a pointer to another component ? How is this different from a property that is actually an embedded component ? Finally how is one notified in a component when another component is destroyed ? I have a managed component called P. Let us say that C is another managed component. If on P I have: __property C * get_CComp(); __property void set_CComp(C *);
1
1866
by: mail.matty | last post by:
Hi All, I've been working on a library which does async Http operations. The library is exposed to COM as I need to access it via SQL2000. Whenever the following line of code executes it generates the 'Object Reference...' error. IAsyncResult result = (IAsyncResult) req.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
7
2649
by: Frank Maxey | last post by:
I am fairly new to VB.Net and am having a curious problem. I have an entry dialog form called from a main form. The calling form needs to check the DialogResult field for an OK response. In my button service in the dialog form, I have: Private Sub btnSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSave.Click
1
1072
by: Nick Dodd via .NET 247 | last post by:
Hi, I have just implemented a reporting system for a web applicationI am responsible for where I work. It basically allows users toenter the report details, click a button and then fires off acall to an asynchronous web service that executes a storedprocedure on SQL Server as a background process. Getting it to run is fine, but what happens if I don't call theEndXXX method once the web service (and stored procedure) havefinished. All the...
4
2805
by: John Allen | last post by:
Hi there, Does anyone know if the standard "PropertyGrid" control is (foreign) language sensitive. If I display an object in the control, and my object has the native .NET "Size" struct as a property (which also contains its own ""Width" and "Height" members), will someone running my app on a German version of Windows for instance still see "Size", "Width" and "Height" in the property window. Or does it get translated into German...
2
3674
by: David | last post by:
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...
2
3176
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 after?
2
2212
by: Morgan Cheng | last post by:
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?
2
7219
by: Dinsdale | last post by:
We have created a object library that implements the INotifyPropertyChanged.PropertyChanged to bubble changes up to higher level classes. For instance, we have a person class that can have relationships with other persons. If there is a change in the relationship (i.e. status, type etc) then we want the PropertyChanged event to fire and notify the top level Person object of that change. The PropertyChanged event is implemented as follows:...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.