大家好,,经过一晚上的奋斗,和,start()和stop()的方法好奇怪,我stop后,再也启动不了,最后参照msdn上timer的用法,我也终于解决了下我自己贴子的问题,
http://bbs.csdn.net/topics/390368476
我的解决方法是::
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_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;
}
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public void hexing_click_select(object source, EventArgs e)
{
//myTimer.Stop();我尝试了下,这个stop不stop都不会影响到ping的时候会卡界面的情况。
z+=1;
if (z < 10)
{
hexing_thread();
myTimer.Enabled = true;////是不是这一句起到了作用?求解
}
else
{
myTimer.Enabled = false;
}
}
public void hexing_thread()
{
int line_count = hexing_date.RowCount;
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);
}
}
private void hexing_check_Click(object sender, EventArgs e)
{
hexing_date.Rows.Clear();
hexing_name_select();
myTimer.Tick += new EventHandler(hexing_click_select);
myTimer.Interval = 1000;
myTimer.Start(); }现在我测试出来的就不会出现界面卡现象,,我有两个问题我想不明白,想问问大家,
①:我这种方法可行么?因为我觉得这个timer也好像是没隔1s就创建了很多线程一样,myTimer.Stop();这句代码没有效果,个人感觉:我现在这个代码的流程和我以前做的那个流程差不多。但是为啥效果会不一样。。
②我测试了下,好像就是这句代码给的效果static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();但是我不懂,这句代码和我从控件界面直接拉下来的timer控件有什么不同的么
http://bbs.csdn.net/topics/390368476
我的解决方法是::
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_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;
}
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public void hexing_click_select(object source, EventArgs e)
{
//myTimer.Stop();我尝试了下,这个stop不stop都不会影响到ping的时候会卡界面的情况。
z+=1;
if (z < 10)
{
hexing_thread();
myTimer.Enabled = true;////是不是这一句起到了作用?求解
}
else
{
myTimer.Enabled = false;
}
}
public void hexing_thread()
{
int line_count = hexing_date.RowCount;
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);
}
}
private void hexing_check_Click(object sender, EventArgs e)
{
hexing_date.Rows.Clear();
hexing_name_select();
myTimer.Tick += new EventHandler(hexing_click_select);
myTimer.Interval = 1000;
myTimer.Start(); }现在我测试出来的就不会出现界面卡现象,,我有两个问题我想不明白,想问问大家,
①:我这种方法可行么?因为我觉得这个timer也好像是没隔1s就创建了很多线程一样,myTimer.Stop();这句代码没有效果,个人感觉:我现在这个代码的流程和我以前做的那个流程差不多。但是为啥效果会不一样。。
②我测试了下,好像就是这句代码给的效果static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();但是我不懂,这句代码和我从控件界面直接拉下来的timer控件有什么不同的么
最好不要加上static,这样所有窗体实例共享一个timer。
去掉static,这代码和你从设计器拖动过来的是一样的。
http://blog.csdn.net/supegg/article/details/6460649
有些关于timer的分析,希望对你有用
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; // myTimer.Enabled = true;
myTimer.Start();
}
public void hexing_click_select(object source, EventArgs e)
{
myTimer.Stop();
//myTimer.Enabled = false;
z+=1;
if (z < 10)
{
hexing_thread();
myTimer.Enabled = true;
}
else
{
int click_all = z - 1;
z = 0;
click_all = 0;
myTimer.Enabled = false;
}
}我按照你的方法,我试了下,,就是不管我用start还是enable=true都无法再暂停后启动
现在我就是卡在这里,,,想又想不懂,,然后百度上查了下,,又没有具体的解决方案,
private void hexing_check_Click(object sender, EventArgs e)
{
hexing_date.Rows.Clear();
hexing_name_select();
z = 0;
hexing_check.Enabled = false;
myTimer.Tick += new EventHandler(hexing_click_select);
myTimer.Interval = 1000;
myTimer.Start(); }
private void hexing_stop_Click(object sender, EventArgs e)
{
z = 0;
myTimer.Stop();
hexing_check.Enabled = true;
} public void hexing_click_select(object source, EventArgs e)
{
myTimer.Stop();
z += 1;
if (z < 10)
{
hexing_thread();
}
else
{
int click_all = z - 1;
z = 0;
click_all = 0;
myTimer.Enabled = false;
}
} public void hexing_thread()
{
int line_count = hexing_date.RowCount;
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);
}
myTimer.Start();
} 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;
}大神,你看看我现在改的对么,,我把 myTimer.Start()放在hexing_thread(),,,我这边测试了下,一切正常,,,但是我有个疑问就是我点击 暂停 按钮后,然后重新点击开始,那个z会变成从2开始每次递增2,然后再暂停开始,又变成从3开始数,每次递增3,为啥z不会变为0(z这里是用来统计我ping了多少次IP,)