Canceling a Process started via System.Diagnostics.Process.Start()
<p>I am writing an application where I have a Process running in a <code>BackgroundWorker</code>.  I would like to support cancellation from the user interface, but I don't see an easy way to do this.</p>

<p>The <code>Process</code> is actually a fairly long running command line exe.  The output is getting redirected asynchronously via the <code>Progress.OutputDataReceived</code> event and is being used to report progress to the GUI.</p>

<pre><code>private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    using (var process = new Process())
    {
        //...

        process.Start()

        //...

        process.WaitForExit();
    }
}

private void CancelWorker()
{
    worker.CancelAsync();
    // where to actually listen for the cancellation???
}
</code></pre>

<p>There doesn't appear to be a way to have the process "listen" for any input from the main thread aside from the <code>StandardInput</code>, but that won't work unless the app itself will response to a specific input to abort.</p>

<p>Is there a way to cancel a process based on a cancel request from the main thread?</p>

<p>For the purposes of my of the EXE running in the process, I can just call <code>Process.Close()</code> to exit without any side-effects, but the <code>Process</code> object is only known to the <code>worker_DoWork()</code> method, so I'll need to keep track of the <code>Process</code> instance for cancellation purposes... that's why I'm hoping there might be a better way.</p>

<p>(if it matters, I am targeting .NET 3.5 for compatibility issues.)</p>
