Launch an external application using ShellExecuteEx and wait for it to initialize

I have an application that should run several other applications in the chain. I run them through ShellExecuteEx . The order of launch of each of the applications is very important, because they depend 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 one possible problem: ShellExecuteEx launches the application and returns almost immediately. A problem can arise when, for example, App1 started correctly, but has not completed some internal tasks, it is not yet ready for use. But ShellExecuteEx already launches App2 , which depends on App1 , and App2 does not start properly, because it needs fully initialized App1 .

Please note that I do not want to wait for the App(N-1) to complete and then launch AppN .

I do not know if this can be solved using ShellExecuteEx, I tried to use

 SEInfo.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC; 

but without any effect.

After starting the AppN application, I have a process handle. If I assume that the application is initialized after creating its main window (all applications have a window), can I somehow put a hook in the message queue and wait until WM_CREATE or maybe WM_ACTIVATE appears? When I click on such a message, my application will know that it can move on.

This is just an idea. However, I do not know how to put such a hook. So if you could help me with this or you have a better idea, that would be great :)

In addition, the solution should work in Windows XP and higher.

Thank you for your time.

Edited

@Cosmic Prund: I do not understand why you deleted your answer? I could try your idea ...

+7
source share
3 answers

Perhaps you will achieve what you need by calling WaitForInputIdle() for each process descriptor returned by ShellExecute() .

Expects the specified process to complete processing of its initial input and waits for user input without waiting for input or before the timeout period has elapsed.

+12
source

If your application has some user initialization logic that does not start in the user interface thread, then WaitForInputIdle may not help. In this case, you will need a mechanism to signal to the previous application that you did the initialization.

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

+3
source

You can always use IPC and Interpocess Synchronization to make an application communicate (and wait if necessary) with each other if you are encoding both applications.

0
source

All Articles