JazziJeff
JazziJeff

Reputation: 731

C# Timer - Need help disabling timer until code executes then restart timer. Currently maxing CPU :(

Basically i have a problem with this timer program I am trying to put together. On starting the program it will utilise a steady 25% CPU which i dont mind, but every time the timer fires it adds another 25% on to the CPU so on the 4th pass im completely maxed out.

I take it I'm not disposing of the timer correctly after it has fired but im new to c# and not really sure how to go about this.

the cope of my program is basically:

any help would be greatly appreciated :)

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    IpCheck();
}

private static void EnableTimer()
{
    System.Timers.Timer aTimer = new System.Timers.Timer();
    // Set the Interval to x seconds.
    aTimer.Interval = 10000;
    aTimer.Enabled=true;
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    aTimer.Enabled = false;
    aTimer.Dispose();
}

ok revised version below - simplified and ruled out the ip check so all it does now is show a message box - this will not even execute anymore :(

public class Timer1        
{

    System.Timers.Timer aTimer = new System.Timers.Timer();
    public static void Main()
    {

        Timer1 tTimer = new Timer1();
        tTimer.EnableTimer();

       }


    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        aTimer.Enabled = false;
        MessageBoxPrint();
        aTimer.Enabled = true;


    }
    private void EnableTimer()
    {

        // Set the Interval to x seconds.
        aTimer.Interval = 10000;
        aTimer.Enabled=true;
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);


    }


    public static void MessageBoxPrint()
    {
        MessageBox.Show("Testing");
    }
}

Upvotes: 0

Views: 2847

Answers (4)

Marcello Faga
Marcello Faga

Reputation: 1204

static System.Timers.Timer aTimer = new System.Timers.Timer();

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {

aTimer.Enabled=false;

            IpCheck();

aTimer.Enabled=true;
        }

        private static void EnableTimer()
        {
            // Set the Interval to x seconds.
            aTimer.Interval = 10000;
            aTimer.Enabled=true;
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        }
private static void DisableTimer()
{


aTimer.Elapsed -= new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = false;


}

NOT TESTED NOT COMPILED, just a sample what i would do in your place, all the added lines are there without no tabs

Upvotes: 0

General Grey
General Grey

Reputation: 3688

The problem is that he is creating a Timer1 inside the Timer1 class so when you load Timer1, it loads another Timer1 which loads another timer1 which loads.... It think you get it

public class Timer1        
{

System.Timers.Timer aTimer = new System.Timers.Timer();
public static void Main()
{

    Timer1 tTimer = new Timer1();//<-this line right here is killing you 
                                 //remove it, as I don't see anyuse for it at all

then in this line

 tTimer.EnableTimer();

just say

EnableTimer();
//or
this.EnableTimer();

You don't need to instantiate the class you are working in, as far as it is concerned it is already instantiated.

Upvotes: 0

madd0
madd0

Reputation: 9323

You're probably looking for something like this:

private static System.Timers.Timer aTimer = new System.Timers.Timer();

// This method will be called at the interval specified in EnableTimer
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    aTimer.Enabled = false;  // stop timer
    IpCheck();
    aTimer.Enabled = true;   // restart timer so this method will be called in X secs
}

private static void EnableTimer()
{
    // Set the Interval to x seconds.
    aTimer.Interval = 10000;
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    aTimer.Enabled=true;  // actually starts timer
}

Upvotes: 2

J&#252;rgen Steinblock
J&#252;rgen Steinblock

Reputation: 31723

I don't quit get, why you have the cpu load, but I would do:

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    ((Timer)source).Enabled = false;
    IpCheck();
    ((Timer)source).Enabled = true;
}

and don't dispose the timer in the method call.

Upvotes: 0

Related Questions