EventSetTimer frequency

To add comments, please log in or register
Jan Flodin
21789
Jan Flodin  

If you have a timer event with EventSetTimer() frequency set to 1 second and the task behind it takes for instance 3 seconds to run, I then presume that the timer will wait for the task to be finalized and not break after 1 second for next OnTimer to be executed.

Please if anyone can confirm.

Thanks in advance.

Marco vd Heijden
Moderator
8148
Marco vd Heijden  

NO.

The timer will overflow and start over executing your code from the beginning..

Your code will never finish.

Set it to 3 if it takes 3 or optimize the code.


Edit:

I could be wrong but it does not make any sense to me.

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(1);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   Print("ONE");
   Sleep(2000);
   Print("TWO");
  }
//+------------------------------------------------------------------+
Jan Flodin
21789
Jan Flodin  
Marco vd Heijden:

NO.

The timer will overflow and start over executing your code from the beginning..

Your code will never finish.

Set it to 3 if it takes 3 or optimize the code.


Edit:

I could be wrong but it does not make any sense to me.

Tested your code and it actually prints both ONE and TWO in the Experts tab. BUT, it prints it every second and not every 2nd second as I would expect. Confusing.


Ex Ovo Omnia
3357
Ex Ovo Omnia  
Jan Flodin:

Tested your code and it actually prints both ONE and TWO in the Experts tab. BUT, it prints it every second and not every 2nd second as I would expect. Confusing.


You may be testing the snippet within an indicator rather than an expert. Sleep in indicators does nothing.

Jan Flodin
21789
Jan Flodin  
Ex Ovo Omnia:

You may be testing the snippet within an indicator rather than an expert. Sleep in indicators does nothing.

Yes, it is an indicator. Sorry that I did not mention it from the beginning.

Marco vd Heijden
Moderator
8148
Marco vd Heijden  

I tested in an EA and it still prints TWO so that is odd to say the least.

I checked the documentation but it does not say anything specific about this issue.

Indra Lukmana
6537
Indra Lukmana  
Jan Flodin:

If you have a timer event with EventSetTimer() frequency set to 1 second and the task behind it takes for instance 3 seconds to run, I then presume that the timer will wait for the task to be finalized and not break after 1 second for next OnTimer to be executed.

Please if anyone can confirm.

Thanks in advance.

it will re execute per second if you set 1.. 
Marco vd Heijden
Moderator
8148
Marco vd Heijden  
Indra Lukmana:
it will re execute per second if you set 1.. 

We just tried that...

Jan Flodin
21789
Jan Flodin  

I have simulated the Sleep() function by using the code below. And when testing it all it starts with printing just ONE. Then every next second it prints TWO ONE, TWO ONE and so on. By doing this I would presume that the OnTimer breaks after one second but at next OnTimer resumes where it stopped during the last run. 


void OnTimer()
 {
   Print("ONE");
   SleepIndicator(3000);
   Print("TWO");
   //check();
 }
  
int SleepIndicator(int milli_seconds)
  {
   uint cont=0;
   uint startTime;
   int sleepTime=0;
   startTime = GetTickCount();
   while (cont<50000)
     {
      cont++;
      sleepTime = (int)(GetTickCount()-startTime);
      if ( sleepTime >= milli_seconds ) break;
     }   
   return(sleepTime);
  }
Marco vd Heijden
Moderator
8148
Marco vd Heijden  

Logically it starts a new cycle every second unaffected by the duration of the total calculation time it will just break and start over.

At least that is what i learned.

Timer should not wait for the calculation to end that would defy the term timer.

For example watchdog timers work on that sole principle.

If it does not retrigger on the next second it's a timeout.
Alain Verleyen
36034
Alain Verleyen  
Marco vd Heijden:

Logically it starts a new cycle every second unaffected by the duration of the total calculation time it will just break and start over.

At least that is what i learned.

Timer should not wait for the calculation to end that would defy the term timer.

Of course not, it works as documented.

For each program no more than one timer can be run. Each mql5 program and each chart has its own queue of events, in which all the newly received events are placed. If the Timer event is present in the queue or is being processed, the new Timer event will not be placed in the queue of the mql5 program.

Wrong this time.

12
To add comments, please log in or register