开发者

Can I have concurrent instances of the async implementation of ping?

I would like to ping many different hosts simultaniously. Does .NET handle this concurrency for me, or must I implement this myself?

For example, the following object internally uses the Async version of ping:

Mypinger.Tracert("microsoft.com");

And I may have a different instance doing the same thing, but to a different host at the same time:

Mypinger.Tracert("google.co开发者_如何学编程m");

What prevents the concurrent Async methods from each instance interfering with each other?

What is the limit on the concurrency?


Yes. Use the Async variants of the various methods on the Ping class.

Adapted from the examples in the Ping class documentation:

private void SendPing()
{
    // ...
    Ping pingSender = new Ping();
    pingSender.PingCompleted +=
             new PingCompletedEventHandler(PingCompletedCallback);

    // the async methods don't block,
    // and execution will continue after the following call
    pingSender.SendAsync(address, userToken);
    // ...
}

private void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
    // ...
    PingReply reply = e.Reply;
    object userToken = e.UserState;
    // ...
}

With regards to your edit and comment, although ICMP communication is typically stateless, ECHO messages (used in pings) maintain connection state, and so know what ping requests triggered a given response. There's really no limit on concurrency, excluding connection swamping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜