Forum Discussion

williamlee's avatar
williamlee
Contributor
14 years ago

2 timer event

Hi



Can I apply 2 timer event in one script/project suite? any suggestion?



thanks
  • Hi



    I want apply 2 timer event in one code since i already use one timer event in the code, I plan to add another one, can I do this, does this cause any conflict of the code?



    thanks
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi William,



    I think (but did not verified this) that technically it might be possible to create more than one timer, but as timers are not synchronized with anything (except with rough time intervals as Windows is not real-time OS) you will have to perform some extra actions to determine what timer was triggered. Why not to put all code into one timer?
  • Hi



    Yes, you are right, I now trying to do this. But somehow I faced one duffculty. I have two method in one timer. The timer trigger time is about 1500ms. One question is how timer operate? What I mean is:

     when

     the code start run, when it hit 1500ms, it go to serve the timer rountine method. When it inside the timer routine method, does the timer still count(1500->1501->....) or it already stop for a while until it finish it tasks inside the timer. Before it go out, the timer reset  back to zero and continue to recount?



    If the timers stop when it stop inside the timer rountine and reset to zero before it go out, why I can;t see this kind of scenario? Does it required any setting to do this?



    thanks
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi William,



    I think that Support will provide you with exact answer, but my educated guess is that timer count is not stopped (like it is for regular Windows timers).

    When I implemented a functionality like this I used a global flag variable to signal whether the timer code is running or not.
  • Hi William,



    As it is stated in the Timer Object help topic, it is possible to add multiple Timer objects to the Timers collection.



    Alexei is right - the timer is not stopped. That's why it's not a very good idea to invoke routines which take long to execute by a timer.
  • HI Allen,



    thanks for your clarification.



    Hi Alexei,



    how to use used a global flag variable to signal whether the timer code is running or not? Can you show me with a sample code?



    thanks


  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi William,



    Some untested DelphiScript-like pseudo-code (just because it supports try/finally) off top of my head:



    var boolInHandler



    procedure OnStartTest(); // can be event handler for the OnStartTest event - see help for more details

    begin

      boolInHandler := false;

      ...

    end;



    procedure TimerHandler();

    begin

      if (boolInHandler) then

        exit;



      boolInHandler := true;

      try

        ...

      finally

        boolInHandler := false;

      end;

    end;