Problem with OnTimer()..

 

Hi,

I want to make an EA tat is called/running once a day - but here in my test-code every hour.

It writes a comment with the time (local) of the last run and the time it should run the next time: one hour later.

BUT as you might see the OnTimer() seem to run every tick? Is that a bug or a feature?

void OnTimer()
  {
   int slp;
   while(!IsStopped()) {
      slp = 3600;
      strTme = StringConcatenate(myName," @ ",TimeToString(TimeLocal(),TIME_SECONDS),"  slp ",(string)slp," sec,  next Check(LocTme): ",TimeToString(TimeLocal()+slp));
      Comment(strTme);
      EventKillTimer(); // to kill existing thread to be safe
      EventSetTimer(slp);
      //Sleep(slp*1000);
      //DebugBreak();
   }
   Comment(strTme,"\n\nEA was stopped at: ",TimeToString(TimeLocal()));
   
  }

It seems as well that after some time the terminal runs at 99% cpu-usage?

Files:
test_3.mq4  3 kb
 
gooly:

I want to make an EA tat is called/running once a day - but here in my test-code every hour [...]

Surely this enters OnTimer() for the first time and then never leaves it, because of the IsStopped() loop?

I don't understand why you're not doing something like the following:

int slp = 3600;

void OnInit()
{
  EventSetTimer(slp);
}

void OnTimer()
{
  strTme = StringConcatenate(myName," @ ",TimeToString(TimeLocal(),TIME_SECONDS),"  slp ",(string)slp," sec,  next Check(LocTme): ",TimeToString(TimeLocal()+slp));
  Comment(strTme);
}
 

Arrgh - you are right.

I first created a script to check the concept and copy and paste the code into the EA...

Reason: