Timer Event Does not Execut in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jwriteclub
    New Member
    • Jun 2007
    • 6

    #1

    Timer Event Does not Execut in C#

    Hello all,

    I have a quick question about timers in C#.

    I have a System.Windows. Forms.Timer timer (the kind you "drag" out of the toolbox.

    It looks something like this:

    [CODE=vbnet]
    public class Class {

    private Timer timer;
    private bool flag = false;

    public Class() {
    timer = new Timer();
    timer.Interval = 400;
    timer.Tick += new System.EventHan dler(timerHandl er);
    }

    public void Function() {
    timer.Start();
    doSomethingWhic hWillTriggerCal lback();
    }

    private void Callback() {
    if (!flag) {
    return;
    }
    flag = false;
    doSomething();
    }

    private void timerHandler() {
    flag = true;
    timer.Stop();
    }
    [/CODE]

    Periodically a function is called which calls a method in another class which will eventually result an event callback. Sometimes this callback executes almost immediately. What I want is that there is a "safe time" during which the event is ignored.

    So, in Function() the timer is started, in 400 ms, flag is set true, and then if the callback executes it does not break out.

    Some unspecified amount of time later, Function() is called again (in theory) restarting the timer.

    So the idea is that if flag is false (a certain amount of time has not elapsed, it breaks out of the event callback.

    Unfortunately, this only works once, after the first time through the timerHandler never gets called, even though timer.Enabled claims to be true.

    Any help would be most appreciated.

    ~ Christopher
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Once you execute the first eventhandler, it tells the timer to stop.
    Where do you tell it to start again?

    If you are just looking for a way to wait for X amount of time try
    Thread.Sleep()

    Comment

    • jwriteclub
      New Member
      • Jun 2007
      • 6

      #3
      Sorry, I was not clear, I edited the original post.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Although I still am unsure what you are trying to do exactly...

        You never appear to RESET the flag boolean back to FALSE, which could probably cause problems

        Comment

        • jwriteclub
          New Member
          • Jun 2007
          • 6

          #5
          Well, I fixed it myself, in that using a System.Timers.T imer works fine.

          I made a mistake on the type in (re resetting the flag).

          As for what I am trying to do . . . Ultimately provide a "safe zone" so that there is a minimum time between when a function is called and when and event from it executes.

          In my particular situation, I want to give a serial device time to finish initializing before I start "working" with it.

          ~ Christopher

          Comment

          Working...