title: Progress bar Start and Stop on Button Click using thread
body: <p>I have created a C# windows application in that I inserted the progress bar.</p>
  
  <p>When a button is clicked the progress bar should appear and then it should start the process for some 2 to 3 seconds and when the process bar is completed it should be hidden.</p>
  
  <p>I have used this code to solve this but its not working.</p>
  
  <p>While the Progress bar is running, the label box that should be like "Generating... 45%" and after completing the label box should be "Generated 100%..", but when I insert the label its showing some errors.</p>
  
  <p>Here is the picture before clicking the Generate button..<br></p>
  
  <p><img src="https://i.stack.imgur.com/nXZmI.jpg" alt="enter image description here"><br>
  On Processing I Should get like this..<br>
  <img src="https://i.stack.imgur.com/Dlzy4.jpg" alt="enter image description here"><br>
  On Final Process id should be like this and the progress bar should hidden..<br>
  <img src="https://i.stack.imgur.com/lBKB8.jpg" alt="enter image description here"><br></p>
  
  <pre><code>        ProgressBar1.Visible = true;
  
          if (isProcessRunning)
          {
              MessageBox.Show("A process is already running.");
              return;
          }
  
          Thread backgroundThread = new Thread(
              new ThreadStart(() =&gt;
              {
                  isProcessRunning = true;
                  for (int n = 0; n &lt; 100; n++)
                  {
                      Thread.Sleep(1);
                      progressBar1.BeginInvoke(new Action(() =&gt; progressBar1.Value = n));
                  }
                  MessageBox.Show("Generated!!!");
  
  
                  if (progressBar1.InvokeRequired)
                      progressBar1.BeginInvoke(new Action(() =&gt; progressBar1.Value = 0));
                  isProcessRunning = false;
              }
  
          ));
  
          // Start the background process thread
          backgroundThread.Start();
  </code></pre>