I'm using a BackgroundWorker object to start a process, the following is inside of my BackgroundWorker.DoWork event:
...
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
while (process.HasExited == false)
{
if (worker.CancellationPending == true)
{
process.Kill();
process.WaitForExit();
CleanUpMethod();
e.Cancel = true;
}
}
....
The main reason i need to kill the process is to delete temporary files used by the process that cannot be deleted while the process is running.
When debugging i've discovered that process.Kill() doesn't seem to be doing anything. The next line process.WaitForExit() just waits until the process runs its normal procedures.
For my purposes of cleaning up resources, my code seems to work fine. But ideally i want to be able to control the lifetime of the process. Could this have something to do with admin permissions? Any ideas?