Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Timer - Need help disabling timer until code executes then restart timer. Currently maxing CPU :(
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>the cope of my program is basically:</p> <ul> <li>Execute some procedures - once completed start timer</li> <li>Wait until timer elapses then start procedures again, disabling the timer until completed</li> </ul> <p>any help would be greatly appreciated :)</p> <pre><code>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(); } </code></pre> <p>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 :(</p> <pre><code>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"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload