2

My code is,

STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
info.dwFlags = STARTF_USESHOWWINDOW;
info.wShowWindow = TRUE;
if (CreateProcess("My_program.exe", command, NULL, NULL, TRUE, 
    CREATE_NEW_CONSOLE, NULL, NULL, &info, &processInfo))
{
    WaitForSingleObject(processInfo.hProcess, INFINITE);
    GetExitCodeProcess(processInfo.hProcess, &exit_code);
    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);
}

When i call this function CreateProcess(), My_program.exe invokes and runs. But my MFC Diolog box gets hang and it shows not responding. Can anyone please help me to avoid this.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
TiYan
  • 125
  • 2
  • 9
  • 9
    You are not just starting a process - you are waiting for it to finish. Did you mean to do that? The program is not hanging in `CreateProcess`, it's hanging in `WaitForSingleObject`. It does what you asked it to do - it waits. – Igor Tandetnik Nov 28 '18 at 04:27
  • You have to start a new thread if you don't want to block the UI – Barmak Shemirani Nov 28 '18 at 04:30
  • 3
    You don't have to start a new thread. Use `MsgWaitForMultipleObjects` to wait on the handle while simultaneously pumping messages. – Peter Ruderman Nov 28 '18 at 04:40
  • 1
    Just an FYI, `info.wShowWindow` is not a boolean. It is an integer expecting one of the `SW_...` flags that the `nCmdShow` parameter of [`ShowWindow()`](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-showwindow) accepts. It just so happens that `TRUE` has the same numeric value as `SW_SHOWNORMAL` (1). – Remy Lebeau Nov 28 '18 at 06:08
  • 1
    @Peter That sort of works until a modal loop is created, that you don't control (e. g. from a popup menu). You won't know that the process has finished, until the modal loop returns. – zett42 Nov 28 '18 at 08:50
  • Possible duplicate of [Waiting on a handle in Windows Thread](https://stackoverflow.com/questions/50759946/waiting-on-a-handle-in-windows-thread) – zett42 Nov 29 '18 at 18:26

0 Answers0