A problem of CPU usage 100% caused by simple while(IsStopped()==false) ?

 

Hello, everyone,

I met a problem while coding in MT4. It is just a simple script that caused the CPU occupied at 100% percent.

I was wondering whether you would be kind enough to enlighten me what is going wrong.

Thank you so much!


//+------------------------------------------------------------------+
//| mySQLTester.mq4 |
//| Copyright ?2006, GP2X |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2006, GP2X"
#property link ""
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
while(IsStopped()==false)
{}

//----
return(0);
}
//+------------------------------------------------------------------+

 
while(IsStopped()==false){} It's called an endless loop. From what I hear, in the old days, those would crash your computer. Watch out for endless loops when using while().
 
What are your computer's technical specs?
 
nanquan:
What are your computer's technical specs?
It will be single core . .. hence 100% CPU use.
 

Use a sleep function within the cycle. It will reduce your processor usage a lot.

while(IsStopped()==false)
 { Sleep(100); }
 
  1. Use
    while( !IsStopped() ){
       :
       if (IsTesting()) break;
       Sleep(100);
       RefreshRates();
    }
    Since you must return in the tester.
  2. Add a Sleep
  3. Add a RefreshRates after the Sleep to see new values.
  4. Why? Just return and wait for the new tick.

 
blackkettle:

Hello, everyone,

I met a problem while coding in MT4. It is just a simple script that caused the CPU occupied at 100% percent.

I was wondering whether you would be kind enough to enlighten me what is going wrong.

Thank you so much!



//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
while(IsStopped()==false)
{}

I think the question has been satisfactorily answered in the preceding posts. I would just comment that you have initially called it a script but the code comments say it is an EA. You have to be a bit careful with the exact terminology you use since these are two different pieces of code, run from different directories, and perform quite differently, even though the code appears to be similar.

For an EA, where the start function is called for every new tick (provided the run from the previous tick has completed), there is little point in using the IsStopped function. When EA operation is disabled the next incoming tick will not generate a call to the start routine anyway. This IsStopped function is very useful for a script which only runs once. If the script needs to be temporarily stopped then the IsStopped function is ideal. In this case the only problem was the lack of a sleep function within the while loop, as mentioned by others. I would not put a RefreshRates within the while loop. That just wastes processing time for no reason, but you do need a RefreshRates as soon as you exit the while loop.

 
dabbler:
I would not put a RefreshRates within the while loop. That just wastes processing time for no reason, but you do need a RefreshRates as soon as you exit the while loop.

That depends on the purpose of the loop. What else is being tested inside. Bid and Ask will not change between calls to start() unless RefreshRates() is called. If the code is looking at the those, you must call RR. If the code is looking at a mutex for example then the call can be outside the loop.

I did ask

Why? Just return and wait for the new tick.
 
WHRoeder:

That depends on the purpose of the loop. What else is being tested inside.

Yes, agreed. But his loop had nothing inside, so the replacement loop also had nothing inside apart from the vital Sleep function to avoid processing overhead :-)
 

Thank you so much, everybody!

I am so appreciative of your kindness and help.

dabbler, I am so grateful of your explanation of the difference between EA and script.


MT4 is great!

Reason: