0

Please find below my piece of code. I crawled through Stack Overflow on how to get notified if a process is terminated and used the suggestions in my code as below(last 3 lines).

    private void button1_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.ShowDialog();
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();            
        //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "/C rep_cmd "+textBox2.Text+" -text "+folderBrowserDialog1.SelectedPath+"\\";
        process.StartInfo = startInfo;
        System.Diagnostics.Process.Start(startInfo);
        if (process.HasExited == true)
           MessageBox.Show("Process done successfully!");            
    }

But, when I run the program, I get a runtime exception saying "No process is associated with this object". Clueless of how to rectify this.

Kindly help.

1 Answers1

1

You will have to spawn a parallel thread to check if the process exit in that parallel thread. the following method will do the trick. To ensure that you dont block the program, invoke it from a parallel thread

    public  void  CheckProc() 
    {
        while (true)
        {
            if (process.HasExited == true)
            {
                MessageBox.Show("Process done successfully!");
                break;
            }

        }
    }

Also go through http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx

Dhawalk
  • 1,257
  • 13
  • 28