How to set timeout in CreateProcess in vc++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • visweswaran2830
    New Member
    • Nov 2009
    • 92

    How to set timeout in CreateProcess in vc++

    Hi,

    I started one process using CreateProcess() in vc++. Now I want to set wait time out for that created process. Let consider, I am starting notepad using the above function, then I asked to handle that process. In this situation, the Notepad does not return any value. so this handling function still holds. I want to set the wait time, If the time exceed then that handled object should release, I have tried with WaitForSingleOb ject and set my time, the timeout function is encounted but the Handle resource doesnot release, here code snippet....


    STARTUPINFO sInfo;
    ZeroMemory(&sIn fo,sizeof(sInfo ));
    PROCESS_INFORMA TION pInfo;
    ZeroMemory(&pIn fo,sizeof(pInfo ));
    sInfo.cb=sizeof (sInfo);
    sInfo.dwFlags=S TARTF_USESTDHAN DLES;
    sInfo.hStdInput =NULL;
    sInfo.hStdOutpu t=wPipe;
    sInfo.hStdError =wPipe;

    char command[1024]; strcpy(command,
    csExecute.GetBu ffer(csExecute. GetLength()));

    //Create the process here.
    if(CreateProces s(0, command,0,0,TRU E,NORMAL_PRIORI TY_CLASS|CREATE _NO_WINDOW,0,0, &sInfo,&pInf o))
    {
    DWORD retVal = WaitForSingleOb ject(pInfo.hPro cess,IE_MAXWAIT );
    if(retVal == WAIT_TIMEOUT)
    {
    MessageBox(NULL ,"TimeOut","Out put",MB_OK|MB_I CONEXCLAMATION) ;
    CloseHandle(pIn fo.hThread);
    CloseHandle(pIn fo.hProcess);
    }
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What do you mean by "Handle resource doesnot release"?

    The code you have looks OK, it creates a process and then waits for it to finish. If it timeut then it deisplays a message.

    The only error is that the process and thread handles are closed only if the wait times out. You always need to close the process and thread handles otherwise you program is leaking resources.

    Comment

    Working...