title: C# process.Kill does not immediately stop a process that is running a batch file
body: <p><strong>The issue</strong>: I am having an issue where I cannot immediately stop a batch file running inside an C# app (process) by using <code>processName.Kill()</code></p>
  
  <p>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. </p>
  
  <p>I am setting up and starting the process like so:</p>
  
  <pre><code>var process = new Process
  {
      StartInfo = new ProcessStartInfo
      {
          UseShellExecute = false,
          FileName = @"C:\Test.bat",
          WorkingDirectory = @"C:\",
          RedirectStandardOutput = true,  
      },
  }; 
  process.Start();
  </code></pre>
  
  <p>The batch file that I am testing with is extremely simple:</p>
  
  <pre><code>echo starting
  timeout /t 4
  echo it worked
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>ct.Register(() =&gt;
  {
      process.Kill();
  });
  </code></pre>
  
  <p>I am sending the standardOutput to a blocking collection of strings line by line like so:</p>
  
  <pre><code>Task.Factory.StartNew(() =&gt;
  {
      while (!streamReader.EndOfStream)
      {
          var line = streamReader.ReadLine();
  
          if (line == null) break;
  
          _blockingCollection.Add(line);
      }
  
      _blockingCollection.CompleteAdding();
  });
  </code></pre>
  
  <p><strong>What I expect to happen</strong>: because the process has been killed by the event raised by the cancellation token, I would expect the <code>streamReader.Readline()</code> 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. </p>
  
  <p>Am I misunderstanding how <code>process.Kill()</code> 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. </p>
  
  <p>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</p>
  
  <p>Thanks</p>