Ping类SendAsync帮助 [英] Ping Class SendAsync Help

查看:253
本文介绍了Ping类SendAsync帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的编程,找不到或知道要搜索什么来调试以SendAsync方法开始的线程。代码使用发送方法很好,但是当使用SendAsync它去waiter.WaitOne(),但我从来没有得到回调(我认为这是所谓的)到myPing_PingCompleted。所以有两个问题如何在启动新线程时调试代码。我正在使用C#Express,所以它可能没有所有的调试工具作为VS。以及我在代码中出现错误的任何想法。
谢谢

  using System; 
使用System.CodeDom;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.Diagnostics;
使用System.Net.NetworkInformation;
使用System.Threading;
使用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 =aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
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;
}


解决方案

我假设你在myPing_PpleCompleted方法中放置了一个breakpoinit,但是在调试模式下,它不会在那里。是对的吗?



代码是否抛出某种错误?如果你通过代码,它是否使用正确的参数调用myPing.SendAsync?



我刚刚尝试了你的代码(没有IPQueue,因为它看起来像你的自定义类)。它在我的尽头工作正常我使用一个有效的IP和不存在的IP。



修改新信息



确定我只是在Windows窗体应用程序中尝试,它不工作。当我在单元测试之前尝试过它。基本上看来,用于渲染窗体并处理事件的线程似乎不能用于创建异步请求(可能是因为它是前台线程)。但是,通过创建另一个线程来执行ping,您可以很容易地绕过它。



实际上,理想情况下,您将不得不这样做。为了使Windows窗体应用程序不在线程忙时锁定,一个好的原则是在一个单独的线程上执行所有后台工作。这将使Windows窗体响应。注意,当尝试使用后台线程访问控件时,将抛出异常。最好把你想要的所有值读入私有变量,然后踢一个将完成所有工作的线程,让线程更新另一组变量,然后让forground线程读取变量并更新控件。 p>

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;
        }

解决方案

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.

这篇关于Ping类SendAsync帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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