I run a process that executes exe on the command line with arguments, and it takes time to complete. In the meantime, I am showing the form as a dialog with a progress bar and a cancel button. When the cancel button is pressed, the process should be interrupted / stopped. I have two ways to do this:
a. Declare an open static object of the Process class in the main form and interrupt it from the execution form when you click the cancel button:
public partial class frmMain : Form { public static Process Process = new Process(); public static bool ExecuteCommand(string sCommandLineFile, string sArguments) { Process.StartInfo.FileName = sCommandLineFile; Process.StartInfo.Arguments = sArguments; Process.StartInfo.CreateNoWindow = true; Process.StartInfo.UseShellExecute = false; Process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; Process.Start(); Process.WaitForExit(); } }
And close / abort the process from the execution window form:
public partial class frmProgress : Form { private void btnCancel_Click(object sender, EventArgs e) { frmMain.Process.Close(); frmMain.Process.Dispose(); } }
B. Or do not call Process.WaitForExit (); and use Process.HasExited instead to check if the process is running and cancel it if the cancel button is clicked:
public static bool IsCancelled = false; Process.StartInfo.FileName = sCommandLineFile; Process.StartInfo.Arguments = sArguments; Process.StartInfo.CreateNoWindow = true; Process.StartInfo.UseShellExecute = false; Process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; while (!Process.HasExited) { Thread.Sleep(100); Application.DoEvents(); if (IsCancelled) { Process.Close(); Process.Dispose(); } } public partial class frmProgress : Form { private void btnCancel_Click(object sender, EventArgs e) { frmMain.IsCancelled = true; } }
What is the right way to do this?
source share