Page 1 of 1

Tiemout in a for loop

Posted: 08 Mar 2020 14:36
by chavannep
Hi there,

I looked for a solution on many forums but I didn't find any suitable ones...

I have several functions (let's say file_1.req file_2.req...file_100.req) I would like to launch with the associated program (let's say calculate.exe). A normal calculation is done within approx. 10s. For some reasons, a file_i.req may make calculate.exe get stuck and the following functions (from i+1 to n) won't be launched (idle case). To avoid this problem, I want to write a batch file which would call calculate.exe for each file_i.req and which would use a 'timeout' (let's say 15s) to perform calculations in a finite time. Moreover it would continue with a 'taskkill' for file_i.req in case 'timeout' elapsed (without waiting for user's commands). I would like my script to continue with the 'i+1' .req file's calculation.
I can not use 'start' for calculate.exe which would open n windows for each req file.
Here is my pseudo-code in the .bat file :

set enabledelayedexpansion
set beginning=file_
set extension=.req

for /L %%g in (1,1,100) do(
set file_req=%beginning%%%g%extension%
call calculate.exe -f %file_req%
:: timeout /T 15 /nobreak
:: taskkill (calculate.exe -f %file_req%)
:: continue
)
endlocal

Thanks a lot for helping !

Pierre

Re: Tiemout in a for loop

Posted: 09 Mar 2020 13:58
by aGerman
chavannep wrote:
08 Mar 2020 14:36
I can not use 'start' for calculate.exe which would open n windows for each req file.
At least you have to use the START command (perhaps START /B) to run your tool asynchronously. Otherwise the timeout command would not be called before calculate.exe has finished.

Untested:

Code: Select all

set "beginning=file_"
set "extension=.req"

for /L %%g in (1,1,100) do(
     start /b calculate.exe -f "%beginning%%%g%extension%"
     timeout /t 15 /nobreak
     taskkill /im calculate.exe /f
)
This will of course invoke the timeout command for every req file.

Steffen

Re: Tiemout in a for loop

Posted: 09 Mar 2020 14:42
by Eureka!
In your original code, there are some commands that will prevent a correct functioning.
Note: just for a learning purpose; I would go with @aGerman's code.

set enabledelayedexpansion should be:

Code: Select all

setlocal enabledelayedexpansion
(setlocal /? or details)

call calculate.exe -f %file_req% should be:

Code: Select all

call calculate.exe -f !file_req!
( SET /? for details )


Note:
In aGerman's solution, you might want to try

Code: Select all

start /wait "Dummy title" calculate.exe -f "%beginning%%%g%extension%"
This will wait until the calculate.exe process is ended. That way you can skip the timeout command.
(start /? for details)

Re: Tiemout in a for loop

Posted: 07 Apr 2020 07:09
by chavannep
Hi Eureka,

You are right. It launches calculate.exe for every file_i.req and it passes through idle ones indeed.
Even if it does use the 'timeout' command, your code is running well !

Thank you very much !
Thanks to German too !

Pierre