The issue: I am having an issue where I cannot immediately stop a batch file running inside an C# app (process) by using processName.Kill()
In my project, the batch file will run several python scripts, taking around 30 minutes to complete in total. It will sporadically dump several lines of output to console, which I am capturing using the associated process' standard output.
I am setting up and starting the process like so:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
FileName = @"C:\Test.bat",
WorkingDirectory = @"C:\",
RedirectStandardOutput = true,
},
};
process.Start();
The batch file that I am testing with is extremely simple:
echo starting
timeout /t 4
echo it worked
I am using a cancellation token from a shared cancellation token source and cancelling after 2000 seconds. I have attached the following event to the token:
ct.Register(() =>
{
process.Kill();
});
I am sending the standardOutput to a blocking collection of strings line by line like so:
Task.Factory.StartNew(() =>
{
while (!streamReader.EndOfStream)
{
var line = streamReader.ReadLine();
if (line == null) break;
_blockingCollection.Add(line);
}
_blockingCollection.CompleteAdding();
});
What I expect to happen: because the process has been killed by the event raised by the cancellation token, I would expect the streamReader.Readline()
method to either immediately return null or throw an exception. Instead, it waits for the timeout in the batch file to complete and then returns the full line that the timeout prints. In my real application, it could be waiting for 10-15 minutes for the next output from the scripts.
Am I misunderstanding how process.Kill()
works? Is it that timeout behaves oddly? In an ideal situation, I would like the stream reader to immediately stop reading and break out of the while loop.
I tried doing this by having an event raised every time the process output received new data however I could not synchronize these to run in the right order as the scripts had a tendency to dump several hundred of lines of output at the same time
Thanks