我有以下代码,它周期性地失败,并显示batch did not finish in 20 seconds错误。
private void executeBat(string batfile)
{
ProcessStartInfo psi = new ProcessStartInfo(batfile);
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.UseShellExecute = false;
psi.RedirectStandardError = false;
psi.RedirectStandardOutput = false;
Process p = Process.Start(psi);
if (!p.WaitForExit(20000))
{
throw new Exception("batch did not finish in 20 seconds.");
}
if (p.ExitCode != 0)
{
throw new Exception("batch failed with exit code " + p.ExitCode + ".");
}
p.Close();
}最初,我认为批处理脚本挂起了,但我添加了代码,它实际上在不到一秒的时间内运行。
下面是批处理脚本:
"c:\Program Files (x86)\SomeThirdParty\SomeExe" >C:\Data\SomeLog.txt 2>&1
set exitcode=%errorlevel%
echo "error return is %exitcode%" >>C:\Data\SomeLog.txt
exit /b %exitcode%日志文件包含"error return is 0"。
.exe运行良好,表明它更新的数据库更新良好。
它每运行500次,就有1次发生这种情况。
我如何调试为什么Process.WaitForExit返回false,尽管其他情况下运行正常?
我的下一步将是删除20000,并放弃处理挂起的进程。
发布于 2018-02-04 01:55:20
这是我学到的.
这是在Task Scheduler上运行的,Task Scheduler正在终止批处理脚本,因为它运行的时间太长。
因此,我将分析的重点从为什么它返回非零转变为为什么批处理脚本没有退出,或者至少没有被检测到正在退出。
我将这个标记为已解决,即使它并未真正解决。这只是一个解释。
https://stackoverflow.com/questions/48469493
复制相似问题