How to avoid prompt when EXE crashes

2019-09-03 19:39发布

I am running an optimization algorithm in MATLAB and I am calling a Batch file (which calls an EXE file) to do a task for each iteration in MATLAB. I want this EXE to run and close before the code in MATLAB continues to the next iteration. So far I have made use of the START /WAIT commands in the Batch file and they work. Here is my example:

@echo off
@TITLE Ostrich - Optimizaton Software Toolkit

REM Launch Ostrich,wait until it completes
START /WAIT "OSTRICH" /MIN Ostrich.exe

echo Done! > OstDone.txt

Now here is my problem. If Ostrich.exe crashes, I get a prompt asking me to debug or close the program. This means I have to be present and watching the screen when I am running this algorithm (not convenient, I am running over 1000 iterations on a sometimes slow EXE). I have to close the program before the MATLAB code can continue.

Now, if I get rid of the /WAIT part, add a timer and do this:

@echo off
@TITLE Ostrich - Optimizaton Software Toolkit

REM Launch Ostrich
START "OSTRICH" /MIN Ostrich.exe

REM Wait 2 seconds
ping localhost -n 2 -w 2500 > nul

REM If it takes more than 2 seconds it has probably crashed. Exit:
if %errorlevel% neq 0 exit /b %errorlevel%

The code above introduces new problems. First, I have to estimate the time it takes for the EXE to run each time (it is not a constant amount of time, so I have to set an upper bound) and the error checking line just doesn't work to close the EXE (probably just closes the batch file which is not ideal).

Under the second code, if the EXE crashes then a prompt comes up. However, the MATLAB loop continues and a new instance of the EXE is run in a new window. If I get enough crashes, I end up with several prompts open at the end of the MATLAB loop. In the context of the overall algorithm, it is not a big deal but it is also not ideal.

Basically, I want to code something that closes and re-runs the EXE file if it crashes, or just closes it (with no debug or close prompt) while still making use of the /WAIT command as it allows me to take advantage of some fast EXE runs (speed of EXE depends on many factors that I may tinker with in its input files).

I am new to using Batch files and any help would be much appreciated.

2条回答
可以哭但决不认输i
2楼-- · 2019-09-03 20:33
@echo off
@TITLE Ostrich - Optimization Software Toolkit

for /L %%a in (1,1,1000) do (
REM Launch Ostrich
START "OSTRICH" /MIN Ostrich.exe

REM Wait 2 seconds
ping -n 2 localhost>nul

REM kill ostrich if it is running or not, and pause for 2 more seconds
taskkill /f /im "ostrich.exe" >nul 2>&1
ping -n 2 localhost>nul
)
查看更多
来,给爷笑一个
3楼-- · 2019-09-03 20:34

You would usually use SetErrorMode() in your code, provided Ostrich.exe is yours.

Otherwise, other than disabling error reporting in general, you may disable error reporting per-application using WerAddExcludedApplication().

查看更多
登录 后发表回答