1

I have a cmd execution takes too long time, I'd like to set a time out for it. After it's time out, then I can go to execute in next line. Is it possible? My cmd is like this.

example1.exe /e:dosomething. 
example2.exe /e:dosomething.

I'd like to set time out for example1.exe, then I could go to execute example2.exe Any method?

Magoo
  • 77,302
  • 8
  • 62
  • 84
Damon
  • 89
  • 2
  • 8
  • possible duplicate of [Windows batch: analogue for \`timeout\` command](http://stackoverflow.com/questions/12394338/windows-batch-analogue-for-timeout-command) – Oleg Mikheev Dec 28 '13 at 10:11
  • Thank you, but it's not what I want, when the example1.exe is executed, the execution won't be ended until 5 minutes later, I want to execute example2.exe after executing example1.exe 1 minutes later. – Damon Dec 28 '13 at 10:30
  • i think the best way for you is programming a multithread C++ program, one of the threads runs the command and the other waits to kill that after dead line, you can compile the program and use the binary file as a command. "argc" and "argv" in the main functions gets commands example1 & 2. if you didn't understand me, tell me and i will help more. – Behnam Safari Dec 28 '13 at 10:44
  • You need to be way more explicit. Example1 starts then what? Wait for some time and start example2? Wait for some time then kill example1 and start example2? Start example2 if example1 terminates or some time has passed- terminating exampl1 or not? Will there be more than one instance of example1 or example2 running concurrently? – Magoo Dec 28 '13 at 12:43
  • Thanks all. Special thanks to Behnma, you remind me to use advanced program, I used C# to start 2 threads, and it works. Thanks very much. – Damon Dec 28 '13 at 14:54

1 Answers1

1

Adapted from a previous answer to handle the cases of killing or keep running the process on timeout

@echo off
    setlocal enableextensions

    rem Case 1 - Start example 1 and kill it after 60 seconds
    start "" /b example1.exe /e:dosomething
    call :timeoutProcess "example1.exe" 60

    rem Test why execution continues
    if errorlevel 1 (
        echo Program has been killed
    ) else (
        echo Program has ended in time
    )

    rem Case 2 - Start example1 and after 60 seconds continue,
    rem          leaving example1 running
    start "" /b example1.exe /e:dosomething
    call :timeoutProcess "example1.exe" 60 1

    rem Test why execution continues
    if errorlevel 1 (
        echo Timeout - Program keeps running
    ) else (
        echo Program has ended in time
    )

    exit /b

:timeoutProcess process timeout [leave]
    rem process = name of process to monitor
    rem timeout = timeout in seconds to wait for process to end
    rem leave   = 1 if process should not be killed
    for /l %%t in (1 1 %~2) do (
        timeout.exe /t 1 >nul
        tasklist | find "%~1" >nul || exit /b 0
    )
    if not "%~3"=="1" taskkill /f /im "%~1" >nul 
    exit /b 1
MC ND
  • 69,615
  • 8
  • 84
  • 126