Inifinite Loop - Sleep getting bypass at stop

 

Hello,


I am using an infinite loop with a "Sleep" on my "OnStart" because I want to keep track of my positions at all time.

The program works great so far, however, once I stop it, the "Sleep(5000)" (which is 5s sleep) seem to brake, and my infinite loop is getting called thousands of times in a few milliseconds, until eventually the program crashes.

Do you have any idea of what could cause this "Sleep" issue ?


Thank you for your help :)

 
Fabylou:

Hello,


I am using an infinite loop with a "Sleep" on my "OnStart" because I want to keep track of my positions at all time.

The program works great so far, however, once I stop it, the "Sleep(5000)" (which is 5s sleep) seem to brake, and my infinite loop is getting called thousands of times in a few milliseconds, until eventually the program crashes.

Do you have any idea of what could cause this "Sleep" issue ?


Thank you for your help :)

you need to post your code otherwise it's just guess work....

but why don't you use a timer event instead, probably a neater solution.

 
void OnStart() {
   while (1) {
      OpenNewPositions();
      CloseOldPositions();
      Print("Infinite Loop");
      Sleep(5000);
   }
}

In here, I'm doing open/close position thing, it seem to work as intended. But as soon as I stop my program, my Print "Infinite Loop" get triggers a huge amount of times in a few millisecond, until the program crashes.
My guess is that there is an issue with MQL5 when a program gets stopped. Or maybe I should add something else in my infinite loop, I don't really know. What's sure is that it happens the second after I stop the program.


After a few research, I thought people were using the same solution as me (using an infinite loop). But if there is a more clean solution, I'm all ears.

 
Fabylou:

In here, I'm doing open/close position thing, it seem to work as intended. But as soon as I stop my program, my Print "Infinite Loop" get triggers a huge amount of times in a few millisecond, until the program crashes.
My guess is that there is an issue with MQL5 when a program gets stopped. Or maybe I should add something else in my infinite loop, I don't really know. What's sure is that it happens the second after I stop the program.


After a few research, I thought people were using the same solution as me (using an infinite loop). But if there is a more clean solution, I'm all ears.

like I said use a timer event instead, much cleaner way to achieve what you want which is to do something every X seconds.

https://www.mql5.com/en/docs/eventfunctions/eventsettimer

set the timer up once in OnInit  and then create an OnTimer() function to include what you want done

https://www.mql5.com/en/docs/event_handlers/ontimer

Documentation on MQL5: Working with Events / EventSetTimer
Documentation on MQL5: Working with Events / EventSetTimer
  • www.mql5.com
The function indicates to the client terminal, that for this indicator or Expert Advisor, events from the timer must be generated with the specified periodicity. Normally, this function must be called from the OnInit() function or from a class constructor. In order to handle events coming from the timer, the Expert Advisor must have the...
 
Fabylou: I am using an infinite loop

Don't do that. Get a tick. See if something needs to be done and do it. Return and wait for a new tick.

Reason: