While loop in crashes when reopening MT4

 

In many of my indicators I record data in folders organised by brokers. I just 'improved' my code for getting the broker name. If I add the indicator, no problems. However, if I close and reopen Mt4, it crashes. Can't see the program, CPU 100%. If I delete the ex4 file, reopen Mt4, compile and add the indicator.. again no problems.

The code is in the Init function, but the problem still occurs if it's in the Start function.

string broker = Broker();
while(broker == "unknown")
   {
   Sleep(10000);
   broker = Broker();
   }

Broker() function gets the broker's name and removes any awkward characters:

string Broker()
   {
   string buffer = AccountCompany();
   if(buffer=="")
      {
      //Alert("can't get broker name even though not offline??, setting broker to 'unknown', continuing...");
      return("unknown");
      }

   ///// remove disallowed characters /////
   int number;
 //                   \    :    *    ?    <    >    |    .    ,    ;    %    /    "  
   string dis[13] = {":", ":", "*", "?", "<", ">", "|", ".", ",", ";", "%", "/", "\""};
   dis[0] = StringTrimRight("\ ");
   
   for(int i = 0; i<ArraySize(dis); i++)
      {
      number = StringFind(buffer,dis[i],0);
      while(number != -1) //6
         {
         if(number==0)
            buffer = "_"+StringSubstr(buffer,number+1,StringLen(buffer)-1);
         else
            buffer = StringSubstr(buffer,0,number)+"_"+StringSubstr(buffer,number+1,StringLen(buffer)-number-1);
         number = StringFind(buffer,dis[i],0);
         }
      }
   return(buffer);
   }

Any ideas? :S

 

I get a similar problem with this code, when the DLLs import option is disabled:

   while(!IsDllsAllowed())
      {
      Alert("You need to enable DLL imports in options!!!");
      Sleep(10000);
      }

I hoped this kind of code would stop the indicator proceeding if the settings aren't right.. but it seems to crash it instead.

 
In general endless loops crashes the terminal and is_not recommended. You'll have better luck with endless loop within Experts and Scripts [ Not_Indicators ] but then again, they're not recommended.
 
Ok thanks, I'll do it another way. It's a shame though.
 
alladir:
Ok thanks, I'll do it another way. It's a shame though.
It's not a shame, you have to debug and fix your code.
 
Sleep() functions are ignored in indicators. Your code loops at full throttle, preventing the MT4 to establish connection to a trading server.
Sleep(10000); //ignored

BTW, instead of

dis[0] = StringTrimRight("\ ");

the usual way is

dis[0] = "\\";
 
alladir:

I get a similar problem with this code, when the DLLs import option is disabled:

I hoped this kind of code would stop the indicator proceeding if the settings aren't right.. but it seems to crash it instead.

Why not put a message on screen for the User and then simply return(0); ?
 
RaptorUK:
Why not put a message on screen for the User and then simply return(0); ?


That's what I will do but it felt more efficient to keep it out of the start function.
 
alladir:

That's what I will do but it felt more efficient to keep it out of the start function.

You should understand, that there is only one thread for indicators. You must proceed the init(), start() and deinit() in indicators as fast as possible, the thread can move to the next indicator only if the current method has finished.
 

How does 'control passing' work with multiple indicators. Mt4 passes control to each, one at a time? E.g. at a new tick, like this?

MT4 --> indicator 1 Start --> MT4 --> indicator 2 Start --> MT4

Sorry, I looked for the answer but couldn't find it.

Reason: