Dobz
Dobz

Reputation: 1213

Timer Won't Stop

I've written this program to automatically gather the IP Addresses that allow me to connect to port 8888.

It works fine until the final loop. Whereby my for loop finishes. But my timer keeps going and outputting:

10.10.10.150 - No
10.10.10.150 - No
10.10.10.150 - No
10.10.10.150 - No

This is my code, after the for loop I try stop the timer, but it doesn't stop.

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();
}

Upvotes: 0

Views: 521

Answers (2)

Jon Egerton
Jon Egerton

Reputation: 41569

From the OPs edits, the answer was to change the catch block to the following:

    } 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");
    }

(see here)

However I wouldn't recommend this fix over correct use of using to manage the timer.

Upvotes: 0

John Nicholas
John Nicholas

Reputation: 4836

try using a using statement to create the timer (and dispose of it no matter the excetion)

using (var timer = new system.timer)
{




}

instead of using an exception handler to do it. Also why the empty exception handler around a huge scope? that is bad you want that removed or scoped to minimum possible..

what is happening is that something is going bang causing your for loop to exit in effect before the timer can be stopped so it keeps firing the event.

in response to jon edgerton the solution shuold be

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;

}

or better yet

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");
    }
}

Upvotes: 3

Related Questions