QA: How can I start another program and wait until it will be finished?

Vassili Philippov (vasja@spbteam.com), August 08, 2001.

Question

I need to start another executable file and wait until it will be finished. How can I do it?

Answer

Use CreateProcess function to start another program and WaitForSingleObject function to wait until the program will be finished.

You could also use RunExecutable and RunExecutableAndWait function of STUtil library that hides details.

Note: Pocket PC doesn't support local file names. All file names should contain full path like _T("\\sindows\\CreateAssetFile.exe").

Source code

Here is a sample code that starts CreateAssetFile.exe program and waits until it will be finished:

// Start CreateAssetFile.exe PROCESS_INFORMATION pi; if (!::CreateProcess(TEXT("\\windows\\CreateAssetFile.exe"), NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi)) { return FALSE; } // Wait until CreateAssetFile.exe will be finished ::WaitForSingleObject(pi.hProcess, INFINITE); return TRUE;

The same functionality implemented using STUtil library:

return CSTUtil::RunExecutableAndWait(TEXT("\\windows\\CreateAssetFile.exe"));