Ping 类 SendAsync 帮助

发布于 2024-08-04 18:17:55 字数 2359 浏览 3 评论 0原文

我是编程新手,无法找到或不知道要搜索什么来调试使用 SendAsync 方法启动的线程。该代码使用 Send 方法可以很好地工作,但是当使用 SendAsync 时,它会转到 waiter.WaitOne() 但我从未收到 myPing_PingCompleted 的回调(我认为这就是它的名称)。所以有两个问题:当代码启动新线程时如何调试代码。我使用的是 C# Express,所以它可能没有 VS 那样的所有调试工具。以及我的代码哪里出了问题的任何想法。 谢谢

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
using System.Net;

private void btnPingAsync_Click(object sender, EventArgs e)
    {
        string bIP = txtStartIP.Text;
        string eIP = txtEndIP.Text;
        int timeOut;
        int cnt = 0;
        if (eIP == null) eIP = bIP;
        Ping myPing = new Ping();
        PingOptions parmPing = new PingOptions();
        AutoResetEvent waiter = new AutoResetEvent(false);
        myPing.PingCompleted +=new PingCompletedEventHandler(myPing_PingCompleted);
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] dataBuffer = Encoding.ASCII.GetBytes(data);
        if (!int.TryParse(txtTimeOut.Text, out timeOut)) timeOut = 120;
        parmPing.DontFragment = true;
        parmPing.Ttl = 32;
        pbQueueStatus.Minimum = 0;
        pbQueueStatus.Step = 10;
        pbQueueStatus.Value = 0;
        pbQueueStatus.Style = ProgressBarStyle.Continuous;




        if (verify.ValidIPAddress(bIP) && verify.ValidIPAddress(eIP))
        {
            IPQueue = build.IPAddressQueue(bIP, eIP);
            pbQueueStatus.Maximum = IPQueue.Count;
            pbQueueStatus.TopLevelControl.UseWaitCursor= true;
            pbQueueStatus.Visible = true;
            while (IPQueue.Count > 0)
            {
                myPing.SendAsync(IPQueue.Dequeue(), timeOut, dataBuffer, parmPing, waiter);
                waiter.WaitOne();
                if (++cnt > 10)
                {
                    pbQueueStatus.PerformStep();
                    cnt = 0;
                }
            }
        }
    }

    private void myPing_PingCompleted(Object sender, PingCompletedEventArgs e)
    {

        PingReply reply = e.Reply;
        ((AutoResetEvent)e.UserState).Set();
        if (reply .Status == IPStatus .Success )
        {
            dosomething;
        }

I am new to programing and can not find or know what to search for to debug the thread that is started with the SendAsync Method. The code works good using the Send Method but when using SendAsync it goes to waiter.WaitOne() but i never get the callback (I think thats what its called) to myPing_PingCompleted. So two questions how do I debug the code when it starts a new thread. I am using C# Express so it may not have all the debuging tools as VS. and any idea where I am going wrong in my code.
Thanks

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
using System.Net;

private void btnPingAsync_Click(object sender, EventArgs e)
    {
        string bIP = txtStartIP.Text;
        string eIP = txtEndIP.Text;
        int timeOut;
        int cnt = 0;
        if (eIP == null) eIP = bIP;
        Ping myPing = new Ping();
        PingOptions parmPing = new PingOptions();
        AutoResetEvent waiter = new AutoResetEvent(false);
        myPing.PingCompleted +=new PingCompletedEventHandler(myPing_PingCompleted);
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] dataBuffer = Encoding.ASCII.GetBytes(data);
        if (!int.TryParse(txtTimeOut.Text, out timeOut)) timeOut = 120;
        parmPing.DontFragment = true;
        parmPing.Ttl = 32;
        pbQueueStatus.Minimum = 0;
        pbQueueStatus.Step = 10;
        pbQueueStatus.Value = 0;
        pbQueueStatus.Style = ProgressBarStyle.Continuous;




        if (verify.ValidIPAddress(bIP) && verify.ValidIPAddress(eIP))
        {
            IPQueue = build.IPAddressQueue(bIP, eIP);
            pbQueueStatus.Maximum = IPQueue.Count;
            pbQueueStatus.TopLevelControl.UseWaitCursor= true;
            pbQueueStatus.Visible = true;
            while (IPQueue.Count > 0)
            {
                myPing.SendAsync(IPQueue.Dequeue(), timeOut, dataBuffer, parmPing, waiter);
                waiter.WaitOne();
                if (++cnt > 10)
                {
                    pbQueueStatus.PerformStep();
                    cnt = 0;
                }
            }
        }
    }

    private void myPing_PingCompleted(Object sender, PingCompletedEventArgs e)
    {

        PingReply reply = e.Reply;
        ((AutoResetEvent)e.UserState).Set();
        if (reply .Status == IPStatus .Success )
        {
            dosomething;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2024-08-11 18:17:55

我假设您在 myPing_PingCompleted 方法中放置了一个断点,但在调试模式下它不会去那里。是这样吗?

代码是否抛出某种错误?如果您单步执行代码,它是否使用正确的参数调用 myPing.SendAsync?

我刚刚尝试了您的代码(没有 IPQueue,因为它看起来像您的自定义类)。对我来说效果很好。我使用了一个有效的IP,而哪个IP不存在。在这两种情况下都效果很好。

编辑新信息

好吧,我刚刚在 Windows 窗体应用程序中尝试过,但它不起作用。当我之前尝试它时,它是在单元测试中。基本上,用于呈现窗口窗体和处理事件的线程似乎无法用于创建异步请求(可能因为它是前台线程)。但是,您可以通过创建另一个线程来执行 ping 操作来轻松解决这个问题。

事实上,理想情况下你无论如何都必须这样做。为了使 Windows 窗体应用程序在线程繁忙时不锁定,一个好的原则是在单独的线程上完成所有后台工作。这将使 Windows 窗体保持响应。但要小心,当尝试使用后台线程访问控件时,将会抛出异常。最好将所需的所有值读取到私有变量中,然后启动一个线程来完成所有工作,让该线程更新另一组变量,然后让前台线程读取变量并更新控件。

I'm assuming that you are putting a breakpoinit in the myPing_PingCompleted method but that when in debug mode its just not going there. Is that right?

Is the code throwing some sort of error? If you step through the code, does it call the myPing.SendAsync with the right parameters?

I just tried your code (without the IPQueue because that looks like your custom class). It works fine on my end. I used one valid IP and what IP that doesn't exist. It worked good in both cases.

Edit for new information

Ok i just tried it in a windows form application and IT DOES NOT WORK. When i tried it before it was in a unit test. Basically it would seem that the thread used to render the windows form and handle the events isn't capable of being used to create async requests (maybe because it is a foreground thread). But, you can get arround it pretty easily by creating another thread to do the ping.

Actually, ideally that's how you would have to do it anyways. In order for the windows form app not to lock up when the thread is busy, a good principle is to do all backround work on a seperate thread. This would keep the windows form responsive. Be careful though, when trying to access Controls using backround threads an exception will be thrown. It is best to read all the values you want into private variables and then kick of a thread that will do all the work, have that thread update another set of variables and then have the forground thread read the variables and update the controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文