2

I work at Ubisoft and we use a very old program to manipulate some files. Since it's legacy software, it's really bad and it may happen that the software has crashed and keeps on running. We sadly don't have access to the code, so we're unable to fix that. I was wondering, is it possible to use System.Diagnostics.Process with a "no log timeout"? Here's what I'm trying to achieve

var legacySoftwareProcess = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        RedirectStandardOutput = true,
        WorkingDirectory = localPackageFolder,
        FileName = CiConfig.DataGeneration.RebuildUnstrippedBatName
     }
};

legacySoftwareProcess.IdleLogTimeout = 5 * 60; // 5 minutes
legacySoftwareProcess.Start();
var output = proc.StandardOutput.ReadToEnd();
legacySoftwareProcess.WaitForExit();

if (legacySoftwareProcess.ExitCode != 0)
{
    Context.LogMessage(output);
    Context.LogError("The process exited with non 0 code");
}
MathieuAuclair
  • 1,229
  • 11
  • 41
  • 1
    "the software has crashed and keeps on running" - Seems contradictory to me. Do you just mean that the software is unresponsive? – Broots Waymb Dec 17 '19 at 17:35
  • 1
    But you could monitor the log file yourself, and if you notice that it hasn't been updated in 5 minutes, do something like: https://stackoverflow.com/questions/3345363/kill-some-processes-by-exe-file-name – Broots Waymb Dec 17 '19 at 17:37
  • Yeah it's unresponsive, and thanks for the tip, I'll try that – MathieuAuclair Dec 17 '19 at 18:38

1 Answers1

2

Rather than using:

var output = proc.StandardOutput.ReadToEnd();

You can listen for the event when output data is received from the process:

proc.OutputDataReceived += ResetTimer;
proc.Start();
proc.BeginOutputReadLine(); // not sure that you should use that as it may read output synchronously (I will check that soon)

And in the handler method ResetTimer, as the method name implies, reset a 5-minute timer:

static void ResetTimer(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        // reset the timer 
    }
}

If timer has elapsed, it means nothing has been outputed for 5 minutes, and you can take action accordingly, ie kill the process.

Guillaume S.
  • 1,515
  • 1
  • 8
  • 21