我是编程新手,找不到或不知道要搜索什么来调试使用 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;
}