定时器不停止
本文关键字:不停止 定时器 | 更新日期: 2023-09-27 18:13:06
我编写了这个程序来自动收集允许我连接到8888端口的IP地址。
在最后一个循环之前,它工作得很好。这样我的for
循环就结束了。但是我的计时器一直在运行并输出:
10.10.10.150 - No
10.10.10.150 - No
10.10.10.150 - No
10.10.10.150 - No
这是我的代码,在for循环之后,我尝试停止计时器,但它没有停止。
protected void PingPython(){
for (int i = 50; i <= 150; i++){
// Try Connect to Python
try{
ip = "10.10.10."+i.ToString();
// Set timer to break Client connection
tConnectTimeout = new System.Timers.Timer(100);
tConnectTimeout.Elapsed += new System.Timers.ElapsedEventHandler(tConnectTimeout_Elapsed);
tConnectTimeout.Start();
// Connect to Client
cli = new TcpClient();
cli.Connect(ip, 8888);
// If it connects, stop the thread
tConnectTimeout.Stop();
tConnectTimeout.Dispose();
Console.WriteLine(ip + " - Yes");
ipAddresses.Add(ip);
cli.Close();
} catch (ObjectDisposedException ex) {
} catch (SocketException ex) {
tConnectTimeout.Stop();
tConnectTimeout.Dispose();
Console.WriteLine(ip + " - No");
}
}
tConnectTimeout.Stop();
btnStart.Sensitive = true;
foreach(string ipa in ipAddresses){
cbAddresses.AppendText(ipa);
}
cbAddresses.Sensitive = true;
}
public void tConnectTimeout_Elapsed(object sender, System.Timers.ElapsedEventArgs e){
//Close the socket
cli.Close();
Console.WriteLine(ip + " - No");
//Stop and dispose timer
tConnectTimeout.Stop();
tConnectTimeout.Dispose();
}
尝试使用using语句来创建计时器(并且无论异常如何都要处理它)
using (var timer = new system.timer)
{
}
而不是使用异常处理程序。另外,为什么在一个巨大的范围周围的空异常处理程序?这是不好的,你希望删除或范围尽可能小…
发生的事情是,在计时器可以停止之前,某些事情会导致你的for循环退出,因此它继续触发事件。
回应Jon edgerton,解决方案应该是
protected void PingPython(){
for (int i = 50; i <= 150; i++){
// Try Connect to Python
try{
ip = "10.10.10."+i.ToString();
// Set timer to break Client connection
tConnectTimeout = new System.Timers.Timer(100);
tConnectTimeout.Elapsed += new System.Timers.ElapsedEventHandler(tConnectTimeout_Elapsed);
tConnectTimeout.Start();
// Connect to Client
cli = new TcpClient();
cli.Connect(ip, 8888);
Console.WriteLine(ip + " - Yes");
ipAddresses.Add(ip);
cli.Close();
} catch (ObjectDisposedException ex) {
} catch (SocketException ex) {
Console.WriteLine(ip + " - No");
}
finally
{
tConnectTimeout.Stop();
tConnectTimeout.Dispose();
}
}
tConnectTimeout.Stop();
btnStart.Sensitive = true;
foreach(string ipa in ipAddresses){
cbAddresses.AppendText(ipa);
}
cbAddresses.Sensitive = true;
}
或者更好的
for (int i = 50; i <= 150; i++)
{
// Try Connect to Python
try
{
ip = "10.10.10."+i.ToString();
// Set timer to break Client connection
using( tConnectTimeout = new System.Timers.Timer(100))
{
tConnectTimeout.Elapsed += new System.Timers.ElapsedEventHandler(tConnectTimeout_Elapsed);
tConnectTimeout.Start();
// Connect to Client
using (cli = new TcpClient())
{
cli.Connect(ip, 8888);
tConnectTimeout.Stop()
Console.WriteLine(ip + " - Yes");
ipAddresses.Add(ip);
cli.Close();
}
}
}
catch (ObjectDisposedException ex)
{
}
catch (SocketException ex)
{
Console.WriteLine(ip + " - No");
}
}
从OPs编辑来看,答案是将catch块更改为以下内容:
} catch (ObjectDisposedException ex) {
//HERE WAS THE PROBLEM. Added these two lines and now working.
tConnectTimeout.Stop();
tConnectTimeout.Dispose();
} catch (SocketException ex) {
tConnectTimeout.Stop();
tConnectTimeout.Dispose();
Console.WriteLine(ip + " - No");
}
(见这里)
但是,我不建议在正确使用using
来管理计时器之前进行此修复。