感觉你用的timer太多,试试看这个
private void hexing_check_Click(object sender, EventArgs e)
{
hexing_date.Rows.Clear(); hexing_name_select(); hexing_time.Enabled = true; }
private void hexing_time_Tick(object sender, EventArgs e)
{
z++;
if (z > 100)//这里设置20次就不会卡,但是20以上就会了。
{
hexing_time.Enabled = false;
int click_all = z - 1;
MessageBox.Show(click_all.ToString());
z = 0;
click_all = 0;
}
else
{
hexing_thread();
Application.DeEvents();
} } public void hexing_name_select()
{
hexing_line hexing = new hexing_line(); for (int i = 0; i < hexing.hexing_name.Count(); i++)
{
hexing_date.Rows.Add();
hexing_date.Rows[i].Cells[0].Value = hexing.hexing_name[i];
hexing_date.Rows[i].Cells[1].Value = hexing.hexing_ip[i];
}
}
public void hexing_thread()
{
int line_count = hexing_date.RowCount;//统计datagridview有多少行,就在下面设置多个线程
Thread[] line_PING = new Thread[line_count];
for (int a = 0; a < line_count; a++)
{
line_PING[a] = new Thread(new ParameterizedThreadStart(hexing_ping));
line_PING[a].Start(a);
}
} public void hexing_ping(object obj)
{ int i = Convert.ToInt32(obj);
Ping pp = new Ping();
PingReply ppResult = pp.Send(Convert.ToString(hexing_date.Rows[i].Cells[1].Value), 10); hexing_date.Rows[i].Cells[2].Value = ppResult.RoundtripTime.ToString();
hexing_date.Rows[i].Cells[3].Value = ppResult.Status.ToString();
hexing_date.Rows[i].Cells[4].Value = z;
}
private void hexing_check_Click(object sender, EventArgs e)
{
hexing_date.Rows.Clear(); hexing_name_select(); hexing_time.Enabled = true; }
private void hexing_time_Tick(object sender, EventArgs e)
{
z++;
if (z > 100)//这里设置20次就不会卡,但是20以上就会了。
{
hexing_time.Enabled = false;
int click_all = z - 1;
MessageBox.Show(click_all.ToString());
z = 0;
click_all = 0;
}
else
{
hexing_thread();
Application.DeEvents();
} } public void hexing_name_select()
{
hexing_line hexing = new hexing_line(); for (int i = 0; i < hexing.hexing_name.Count(); i++)
{
hexing_date.Rows.Add();
hexing_date.Rows[i].Cells[0].Value = hexing.hexing_name[i];
hexing_date.Rows[i].Cells[1].Value = hexing.hexing_ip[i];
}
}
public void hexing_thread()
{
int line_count = hexing_date.RowCount;//统计datagridview有多少行,就在下面设置多个线程
Thread[] line_PING = new Thread[line_count];
for (int a = 0; a < line_count; a++)
{
line_PING[a] = new Thread(new ParameterizedThreadStart(hexing_ping));
line_PING[a].Start(a);
}
} public void hexing_ping(object obj)
{ int i = Convert.ToInt32(obj);
Ping pp = new Ping();
PingReply ppResult = pp.Send(Convert.ToString(hexing_date.Rows[i].Cells[1].Value), 10); hexing_date.Rows[i].Cells[2].Value = ppResult.RoundtripTime.ToString();
hexing_date.Rows[i].Cells[3].Value = ppResult.Status.ToString();
hexing_date.Rows[i].Cells[4].Value = z;
}
是我的线程问题,,还是需要给form1设置线程,,求代码指导
});
你说的这个怎么做的啊
在hexing_time_Tick中放入hexing_time.stop();
然后在hexing_ping中最后加入hexing_time.start();
但是还是不行。就只会ping一次,然后就不会ping了
{ int i = Convert.ToInt32(obj);
Ping pp = new Ping();
PingReply ppResult = pp.Send(Convert.ToString(hexing_date.Rows[i].Cells[1].Value), 10);
hexing_date.Rows[i].Cells[2].Value = ppResult.RoundtripTime.ToString();
hexing_date.Rows[i].Cells[3].Value = ppResult.Status.ToString();
hexing_date.Rows[i].Cells[4].Value = z; Thread th = new Thread(new ThreadStart(time_start));
th.Start();
// hexing_time.Stop();
// hexing_time.Start();
} private void time_start()
{
hexing_time.Start();
}
按照你这么速度累计,一段时间你的程序就会消耗完资源卡死了。
你每700毫秒应该是发送一个ping包吧。
下次再发送应该判断上一次的执行是否完成。如果没完成的话再发送ping就会出现累积的情况了。