Be Kind To New Users
Be Kind To New Users

Reputation: 10063

Process.WaitForExit returns false

I have the following code that periodically fails with the batch did not finish in 20 seconds error.

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();
}

Initially I thought that batch script was hanging but I added code and it is actually running in less than a second.

Here is the batch script:

"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%

The log file contains "error return is 0".

The .exe runs fine as indicated that the database it updates is updated fine.

This happens about 1 out of every 500 times it runs.

How can I debug why Process.WaitForExit is returning false despite otherwise running fine?

My next step will be to remove the 20000 and forgo the handling of a hung process.

Upvotes: 2

Views: 322

Answers (1)

Be Kind To New Users
Be Kind To New Users

Reputation: 10063

Here is what I learned....

This was running on Task Scheduler and Task Scheduler is killing the batch script because it was running to long.

So, I am changing the focus of my analysis from why it is returning non-zero to why the batch script is not exiting or at least is not being detected as exiting.

I am marking this as as resolved even tho it is not really resolved. It is just explained.

Upvotes: 1

Related Questions