我已经编写了这个程序来自动收集允许我连接到端口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();
}
答案 0 :(得分:3)
尝试使用using语句创建计时器(并处理它而不管除外)
using (var timer = new system.timer)
{
}
而不是使用异常处理程序来执行此操作。 为什么围绕一个巨大的范围的空异常处理程序?这是坏的,你想删除或范围尽可能最小..
发生的事情是,在定时器停止之前,某些东西会导致你的for循环退出,因此它会一直触发事件。
回应jon edgerton解决方案shuold是
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");
}
}
答案 1 :(得分:0)
从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");
}
(见here)
但是,我建议不要正确使用using
来管理计时器。