title: How to run new process after the old process finish, without hang the application
body: <p>I working on Visual Studio 2010, C# (.Net 4).</p>
  
  <p>I have an application that contain a DataGridView with two columns. Each column contain argument for the program I want to run.</p>
  
  <p>Now I want to run from my C# application another application with parameters from the data grid view.
  In additional, I want to wait 3 seconds before running new process.</p>
  
  <p>I try the follow code, but the process runs parallelly and not one after one:</p>
  
  <pre><code>private static Mutex mut = new Mutex();
  
  public void runProgram(string executablePath, string argu1, string argu2)
  {
      mut.WaitOne();
  
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.WorkingDirectory = Path.GetDirectoryName(executablePath);
      startInfo.FileName = "CMD.exe";
      startInfo.Arguments = "/C " + set.ExecutablePath + " "
                              + argu1 + " " + argu2;
  
      try
      {
          Process p = Process.Start(startInfo);
          //p.WaitForExit();  // don't start the next process until the first finish -&gt; not good, stuck the app
      }
      catch
      {
          MessageBox.Show("ERROR: unsuccess to run the applicition");
      }
  
      mut.ReleaseMutex();
  }
  
  private void bu_RunProgram_Click(object sender, EventArgs e)
  {
      if (!File.Exists(set.ExecutablePath))
      {
          MessageBox.Show("The executable file doesn't exist\nPlease select right executable file in the settings form", "Error");
          return;
      }
  
      int progRow;
      foreach (DataGridViewRow dgvr in dg_ParametersToRun.Rows)
      {
          progRow = dgvr.Index;
          string argu1 = dg_ParametersToRun.Rows[progRow].Cells[0].FormattedValue.ToString();
          string argu2 = dg_ParametersToRun.Rows[progRow].Cells[1].FormattedValue.ToString();
  
          Thread threadProgRun= new Thread(() =&gt; runProgram(set.ExecutablePath, argu1, argu2));
          threadProgRun.Start();
  
          Thread.Sleep(3000); // Need to come to this line only when the previous thread finish...
      }
  }
  </code></pre>
  
  <p>To be honest, I've never worked with threads..</p>
  
  <p>Thank you!</p>
  
  <p>UPDATE CODE (According the suggestion):</p>
  
  <pre><code>// Aid variable
  int paramRowIndex;
  
  public void runNextParameters(object sender, EventArgs e)
  {
      paramRowIndex++;
      if (paramRowIndex &gt; dg_RunListTests.Rows.Count) return;   finish run all parameters
  
              //run the test
      string argu1 = dg_RunListTests.Rows[paramRowIndex].Cells[0].FormattedValue.ToString();
      string argu2 = dg_RunListTests.Rows[paramRowIndex].Cells[1].FormattedValue.ToString();
      runParameters(set.ExecutablePath, componenet, test);
  }
  
  public void runParameters(string executablePath, string argu1, string argu2)
  {
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.WorkingDirectory = Path.GetDirectoryName(executablePath);
      startInfo.FileName = "CMD.exe";
      startInfo.Arguments = "/C " + set.ExecutablePath +  " "
                          + argu1 + " " + argu2;
      try
      {
          Process p = Process.Start(startInfo);
          p.Exited += runNextParameters;
      }
      catch
      {
          MessageBox.Show("Unsuccess to run the test again");
      }
  }
  
  private void bu_RunParameters_Click(object sender, EventArgs e)
  {
      if (!File.Exists(set.ExecutablePath))
      {
          MessageBox.Show("The executable file doesn't exist\nPlease select right     executable file in the settings form", Error);
          return;
      }
  
      paramRowIndex = 0;
      runNextParameters(sender, e);
  }
  </code></pre>