why do ChartApplyTemplate only work on the second attempt?

 

I have an exe file that i created in python (Start.exe) which when executed manually generates two files "a data file" and "a template file".
now i am trying to automate that process by executing "Start.exe" from the indicator and then Load the template that the "Start.exe" generated.

But when I run the indicator , only then it loads the template on the second attempt and not on the first attemp (in second attempt it  creates the files and then apply the template ).
in the templates folder i can see that it generates  "a data file" and "a template file" on the first attempt.
which means the "
ShellExecuteW()" command is working fine and "Start.exe" is executing correctly, so it should sleep for 5 seconds and then apply the newly created chart template

But thats not happening , it creates the template and then nothing , and when i attach the indicator to the chart again (second time)
only then it apply the template.
Here is the indicator code:

      Sleep(1000);
      string  DataPath=TerminalInfoString(TERMINAL_DATA_PATH);
      Alert( DataPath);
      int get = ShellExecuteW(0,"Open","start.exe",NULL,terminalDataPath+"\\templates",5);
      Print("Shell ",get);

      Sleep(5000);
      ChartApplyTemplate(0,"do not Delete this") ;

Same case with 3rd and 4th attempt , meaning this attempt will only create the files , and wont load the template 
and 4th attempt will create the files and apply the template 

 
Because the file is not ready jet. Sleep does not work in indicators, as much as I know.