Start external app with ShellExecuteEx and wait un

2019-02-09 13:04发布

I have an application which needs to run several other applications in chain. I am running them via ShellExecuteEx. The order of running each of the apps is very important cause they are dependant on each other. For example:

Start(App1);

If App1.IsRunning then
  Start(App2);
If App2.IsRunning then
  Start(App3);
.........................
If App(N-1).IsRunning then
  Start(App(N));

Everything works fine but there is a one possible problem: ShellExecuteEx starts the application, and return almost immediately. The problem might arise when for example App1 has started properly but has not finished some internal tasks, it is not yet ready to use. But ShellExecuteEx is already starting App2 which depends on the App1, and App2 won't start properly because it needs fully initialized App1.

Please note, that I don't want to wait for App(N-1) to finish and then start AppN.

I don't know if this is possible to solve with ShellExecuteEx, I've tried to use

SEInfo.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC;

but without any effect.

After starting the AppN application I have a handle to the process. If I assume that the application is initialized after its main window is created (all of Apps have a window), can I somehow put a hook on its message queue and wait until WM_CREATE appears or maybe WM_ACTIVATE? In pressence of such message my Application would know that it can move on.

It's just an idea. However, I don't know how to put such hook. So if you could help me in this or you have a better idea that would be great:)

Also, the solution must work on Windows XP and above.

Thanks for your time.

Edited

@Cosmic Prund: I don't understand why did you delete your answer? I might try your idea...

3条回答
时光不老,我们不散
2楼-- · 2019-02-09 13:36

You can probably achieve what you need by calling WaitForInputIdle() on each process handle returned by ShellExecute().

Waits until the specified process has finished processing its initial input and is waiting for user input with no input pending, or until the time-out interval has elapsed.

查看更多
在下西门庆
3楼-- · 2019-02-09 13:46

If your application has some custom initialization logic that doesn't run in UI thread then WaitForInputIdle might not help. In that case you need a mechanism to signal the previous app that you're done initializing.

For signaling you can use named pipes, sockets, some RPC mechanism or a simple file based lock.

查看更多
Explosion°爆炸
4楼-- · 2019-02-09 13:57

You can always use IPC and Interpocess Synchronization to make your application communicate with (and wait for, if needed) each other, as long as you code both applications.

查看更多
登录 后发表回答