How to set timeout in CreateProcess in vc++

Discussion in 'Programming' started by visweswaran28, Jan 21, 2010.

  1. #1
    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 WaitForSingleObject and set my time, the timeout function is encounted but the Handle resource doesnot release, here code snippet....


    STARTUPINFO sInfo;
    ZeroMemory(&sInfo,sizeof(sInfo));
    PROCESS_INFORMATION pInfo;
    ZeroMemory(&pInfo,sizeof(pInfo));
    sInfo.cb=sizeof(sInfo);
    sInfo.dwFlags=STARTF_USESTDHANDLES;
    sInfo.hStdInput=NULL;
    sInfo.hStdOutput=wPipe;
    sInfo.hStdError=wPipe;

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

    //Create the process here.
    if(CreateProcess(0, command,0,0,TRUE,NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo))
    {
    DWORD retVal = WaitForSingleObject(pInfo.hProcess,IE_MAXWAIT);
    if(retVal == WAIT_TIMEOUT)
    {
    MessageBox(NULL,"TimeOut","Output",MB_OK|MB_ICONEXCLAMATION);
    CloseHandle(pInfo.hThread);
    CloseHandle(pInfo.hProcess);
    }
    }
     
    visweswaran28, Jan 21, 2010 IP
  2. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Insert following code after MessageBox(.....

    
    if (TerminateProcess(pInfo.hProcess, 0)){
          // Success
    }
    else{
          // Failed to terminate process
    }
    
    Code (markup):
     
    NeoCambell, Jan 21, 2010 IP