我可以使用IAsyncResult.IsCompleted属性来了解是否已调用Endxxx? [英] can I use IAsyncResult.IsCompleted Property to know if Endxxx was already called?

查看:74
本文介绍了我可以使用IAsyncResult.IsCompleted属性来了解是否已调用Endxxx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

有人知道我是否可以使用IconyncResult.IsCompleted属性

IAsyncResult从Socket.Beginxxx方法返回以确定是否

Endxxx方法已经被称为perior或者没有?

我有一些方法在我调用begin之后返回IAsyncResult ...(例如,连接



我想知道我是否应该在

方法之外调用EndConnect。

谢谢

Hi all,
someone know if I can use the IAsyncResult.IsCompleted 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

推荐答案




您应该随时致电EndConnect。


-

Dave Sexton


" semedao" < se ***** @ community.nospamwrote in message

news:%2 ****************** @ TK2MSFTNGP05.phx.gbl ...
Hi,

You should always call EndConnect regardless.

--
Dave Sexton

"semedao" <se*****@community.nospamwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...

大家好,

有人知道我是否可以使用IAsyncResult.IsCompleted属性

IAsyncResult从Socket.Beginxxx方法返回以确定

Endxxx方法是否已经调用perior?

我有调用begin后返回IAsyncResult的方法... (例如,连接



我想知道我是否应该在

方法之外调用EndConnect。

谢谢
Hi all,
someone know if I can use the IAsyncResult.IsCompleted 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



嗨Semedao,


根据我的理解,你的主要线程正在使用Socket.BeginXXX方法执行一些异步的

套接字操作。在这个主线程中调用

Socket.BeginXXX方法后,你想知道你是否应该检查IAsyncResult.IsCompleted属性以确定操作是否为

完成,所以不需要在主线程中调用Endxxx方法。

如果我误解了你的问题背景,请随时告诉我,

谢谢。


通常,在Socket异步编程模型中,在你调用

Socket.BeginConnect之前,你可以创建实现
的acallback方法。
AsyncCallback委托并将其名称传递给BeginConnect方法,作为

第二个参数。此回调方法将在单独的线程中执行

,并在BeginConnect返回后由系统调用。回调方法

必须接受BeginConnect方法返回的IAsyncResult作为

参数。在回调方法中获取Socket后,您可以调用EndConnect方法成功完成连接尝试。

注意:EndConnect在线程池线程中执行将阻止

直到连接。


因此您的主线程无需调用EndConnect方法,但它

是在线程池线程中执行的回调方法应该调用

EndConnect方法。


我认为你的主要关注点可能是:在主线程中,在调用之后

Socket.BeginXXX方法,你应该怎么做才知道这个操作已经完成了
。由于回调线程知道操作状态,通常,您可以使用事件在主要的
线程和回调执行线程之间进行同步和通信。下面的文章

中的示例代码演示了逻辑

Asynchronous Client Socket Example
http://msdn2.microsoft.com/en-us/library/bew39x2a.aspx


最后,如果你想知道内部工作,通过使用Reflector,你会看到,bocket.EndConnect方法主要是调用

LazyAsyncResult.WaitForCompletion(true),其关键代码片段列出

以下:


私有对象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);

}

}

.....

尝试

{

event1.WaitOne(-1,false);

......

}

是的,此方法检查IsCompleted和IsCompleted。属性,如果是真的,

Socket.EndConnect方法将返回而不会休眠。如果没有,则表示

连接操作仍处于未决状态,它将获得内部m_Event

并等待此事件发出信号。注意:m_Event在

Socket.BeginConnect方法中创建,并在异步

操作完成时发出信号。


So Socket.EndConnect方法确实检查IsCompleted物业

内部,但是,它将消除我们对IsCompleted

属性的循环检查,但等待内部事件以获取通知。


希望这会有所帮助。


祝你好运,

Jeffrey Tan

Microsoft在线社区支持
============================================= =====

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========

此帖子按原样提供。没有保证,也没有赋予任何权利。

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/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.




我知道,但问题是如何确定我是否在其他地方已经叫过Endconnect

这个套接字

而不是写一些标志,我想在

IasyncResult中使用一些东西..

因为第二次调用EndConnect会抛出异常。

" Dave Sexton" < dave @jwa [remove.this] online.comwrote in message

news:%2 ****************** @ TK2MSFTNGP05.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******************@TK2MSFTNGP05.phx.gbl...




你应该随时致电EndConnect。


-

Dave Sexton


" semedao" < se ***** @ community.nospamwrote in message

news:%2 ****************** @ TK2MSFTNGP05.phx.gbl ...
Hi,

You should always call EndConnect regardless.

--
Dave Sexton

"semedao" <se*****@community.nospamwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...

>大家好,
有人知道我是否可以使用从Socket返回的IAsyncResult的IAsyncResult.IsCompleted属性。 Beginxxx方法确定是否已将perxxx方法调用为perior?
我在调用begin后返回IAsyncResult的方法...
(例如连接)
我想知道我是否应该在
方法之外调用EndConnect。
谢谢
>Hi all,
someone know if I can use the IAsyncResult.IsCompleted 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




这篇关于我可以使用IAsyncResult.IsCompleted属性来了解是否已调用Endxxx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆