请帮助我将延迟添加到Continues()方法。
namespace NetworkUtilities
{
public partial class Form1 : Form
{
Thread th1;
public Form1()
{
InitializeComponent();
th1 = new Thread(Continues);
}
private void Continues()
{
try
{
for (int j = 0; j < 4; j++)
{
Ping myping1 = new Ping();
PingReply reply1 = myping1.Send(txtIpDns.Text, 4000);
rtextPingResult.AppendText("\nReply from " + reply1.Address + ": " + "bytes=32" + " time=" + reply1.RoundtripTime + "ms TTL " + reply1.Options.Ttl);
Thread.Sleep(500);
}
}
catch (Exception ex)
{
}
}
private void btnPing_Click(object sender, EventArgs e)
{
try
{
Form1 fm = new Form1();
btnPing.Text = "Waiting.....";
btnPing.Enabled = false;
rtextPingResult.Clear();
//int packetSize = int.Parse(txtPacketSize.Text);
Ping myping = new Ping();
PingReply reply = myping.Send(txtIpDns.Text, 4000);
rtextPingResult.AppendText("Pinging " + txtIpDns.Text + " [" + reply.Address + "]" + " with 32 bytes of data:");
if (reply != null)
{
th1.Start();
rtextPingResult.BackColor = Color.Green;
btnPing.Enabled = true;
btnPing.Text = "Ping";
//t1.Abort();
}
}
catch (Exception ex)
{
rtextPingResult.Text = "";
//rtxtResult.AppendText("Error"+ex.InnerException.ToString);
rtextPingResult.BackColor = Color.Red;
rtextPingResult.AppendText("Error : Please check the IP address or DNS type Correctly or Check the Network" +"\n" +ex.Message);
//MessageBox.Show(ex.ToString());
btnPing.Enabled = true;
btnPing.Text = "Ping";
}
}
答案 0 :(得分:0)
你没有解释这个问题,但我猜你想在Continues
完成之后发生颜色变化等?
你可以这样做
namespace NetworkUtilities
{
public partial class Form1 : Form
{
Thread th1;
public Form1()
{
InitializeComponent();
th1 = new Thread(Continues);
}
private void Continues()
{
try
{
for (int j = 0; j < 4; j++)
{
Ping myping1 = new Ping();
PingReply reply1 = myping1.Send(txtIpDns.Text, 4000);
rtextPingResult.AppendText("\nReply from " + reply1.Address + ": " + "bytes=32" + " time=" + reply1.RoundtripTime + "ms TTL " + reply1.Options.Ttl);
Thread.Sleep(500);
}
Invoke((MethodInvoker) delegate {
rtextPingResult.BackColor = Color.Green;
btnPing.Enabled = true;
btnPing.Text = "Ping";
});
}
catch (Exception ex)
{
}
}
private void btnPing_Click(object sender, EventArgs e)
{
try
{
Form1 fm = new Form1();
btnPing.Text = "Waiting.....";
btnPing.Enabled = false;
rtextPingResult.Clear();
//int packetSize = int.Parse(txtPacketSize.Text);
Ping myping = new Ping();
PingReply reply = myping.Send(txtIpDns.Text, 4000);
rtextPingResult.AppendText("Pinging " + txtIpDns.Text + " [" + reply.Address + "]" + " with 32 bytes of data:");
if (reply != null)
{
th1.Start();
//t1.Abort();
}
}
catch (Exception ex)
{
rtextPingResult.Text = "";
//rtxtResult.AppendText("Error"+ex.InnerException.ToString);
rtextPingResult.BackColor = Color.Red;
rtextPingResult.AppendText("Error : Please check the IP address or DNS type Correctly or Check the Network" +"\n" +ex.Message);
//MessageBox.Show(ex.ToString());
btnPing.Enabled = true;
btnPing.Text = "Ping";
}
}
当另一个线程中的循环结束时,它会更改颜色等,请注意使用Invoke
,因为您无法从另一个线程更改UI元素,因此Invoke
使其在UI内部发生线程。
BTW避免
catch (Exception ex)
{
}
它只会咬你,因为你不会知道坏事正在发生。你想知道什么时候发生了不好的事情,所以你可以解决它们。