Hold the program execution till ShellExecute is completed

 

I have the following code:  

#import "shell32.dll" 

int ShellExecuteW(int hwnd,string operation,string file,string parameters,string directory,int showCmd); 

#import
void OnInit()
{
string strParameters = "/c terms.exe";

    int result = ShellExecuteW(0, "open", "cmd.exe", strParameters , NULL, 1);

    if (result <= 32) Print("Shell Execute Failed: ", result);

//Read a file generate by ShellExecuteW() function and then set timer

EventSetTimer(1);
}
void OnTimer()
{

Print("Hello");
}

The code is working fine. But after ShellExecuteW() command, I am getting a file which I have to load in OnInit(). But the execution doesn't wait for the ShellExecuteW() to complete and moves on. 

Kindly, let me know how I can hold the program to complete the commandline ShellExecute and then read file and then set the Timer for further execution.

 
jaffer wilson:

I have the following code:  

The code is working fine. But after ShellExecuteW() command, I am getting a file which I have to load in OnInit(). But the execution doesn't wait for the ShellExecuteW() to complete and moves on. 

Kindly, let me know how I can hold the program to complete the commandline ShellExecute and then read file and then set the Timer for further execution.

If you want to wait for the process to exit, then instead of ShellExecuteW() you should use something like

if (CreateProcessW()) WaitForSingleObject();

It takes a bit more time coding it, though.

 
Ex Ovo Omnia:

If you want to wait for the process to exit, then instead of ShellExecuteW() you should use something like

It takes a bit more time coding it, though.

A good solution.. But I couldn't find how it can be implemented in MQl5.

 

jaffer wilson:

But the execution doesn't wait for the ShellExecuteW() to complete and moves on. 

Kindly, let me know how I can hold the program to complete the commandline ShellExecute and then read file and then set the Timer for further execution.

After invoking ShellExecuteW(), poll (watch, wait) for the existence of the file that will be created. Do this from within your MQL code. When the file exists, continue execution in your MQL code.

Without knowing what exactly you are trying to do, and speaking off the top of my head . . .

  • Use OnInit() only to fire off the ShellExecuteW() process.
  • Check for the file's existence using OnTimer(), like every second.
  • When the file appears, fire off a custom event to read/process/handle the file contents.

 
Anthony Garot:

After invoking ShellExecuteW(), poll (watch, wait) for the existence of the file that will be created. Do this from within your MQL code. When the file exists, continue execution in your MQL code.

Without knowing what exactly you are trying to do, and speaking off the top of my head . . .

  • Use OnInit() only to fire off the ShellExecuteW() process.
  • Check for the file's existence using OnTimer(), like every second.
  • When the file appears, fire off a custom event to read/process/handle the file contents.

Thank you sir for your help.