title: C# WinForms - run progressbar in new thread while waiting for process to stop in main thread
body: <p>I need a little help with my program. I am trying to show progressbar in marquee mode, when my main thread (Form) starts a new process and waits for process exit. This means I start pdflatex to compile TEX file and I show new form with progressbar until process WaitForExit() method is done. And I need to know if Im doing it in right way or there is another better method. I have a class called Progress which extends Form and is used for new thread.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Diagnostics;
  using System.Windows.Forms;
  using System.Drawing;
  
  namespace Measuring
  {
  public class Progress : Form
  {
      public Progress(Form form)
      {
          InitializeComponent(form);
      }
  
      private void InitializeComponent(Form parent)
      {
          this.FormBorderStyle = FormBorderStyle.None;
          this.Font = parent.Font;
          this.Size = new Size(300, 40);
          this.StartPosition = FormStartPosition.Manual;
          this.Location = new Point(parent.Left + ((parent.Width - this.Width) / 2), (parent.Top + ((parent.Height - this.Height) / 2)));
  
          ProgressBar progressbar = new ProgressBar();
          Label label = new Label();
  
          label.AutoSize = true;
          label.Text = "Converting file to pdf";
          label.Dock = DockStyle.Top;          
  
          progressbar.Dock = DockStyle.Bottom;
          progressbar.Maximum = 100;
          progressbar.Minimum = 0;
  
          progressbar.ForeColor = Color.Green;
          progressbar.Style = ProgressBarStyle.Marquee;
          progressbar.MarqueeAnimationSpeed = 10;
  
  
          this.Controls.Add(label);
          this.Controls.Add(progressbar);
      }
  
      public void Start()
      {
          this.ShowDialog();
      }
  
      public void Stop()
      {
          this.Close();
      }
  }
  }
  </code></pre>
  
  <p>Now I have streamwrite method where at the end i call this:</p>
  
  <pre><code>        Process p = new Process();
          p.StartInfo.FileName = "pdflatex";
          p.StartInfo.Arguments = save.FileName;
          p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  
          Progress prg = new Progress(this);
          Thread t = new Thread(prg.Start);
  
          try
          {
  
              if (p.Start())
              {
                  t.Start();
                  p.WaitForExit();
                  prg.Stop();
                  if (p.ExitCode != 0)
                  {
                      MessageBox.Show("Conversion to pdf using LaTeX failed!" + Environment.NewLine + "No output file produced.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  }
              }
          }
          catch
          {
              MessageBox.Show("Pdflatex is not installed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
  </code></pre>
  
  <p>It seems to be OK but I dont know if it is really safe or not. I know that all exceptions are not handled but main things are Start() and Stop() methods.</p>
  
  <p>Thanks a lot.</p>