title: C# Threading using invoke, freezing the form
body: <p>I'm trying to use threads and prevent the program from freezing while the thread is busy. It should show the progress (writing of 0's / 1's) and not just show the result after its done, freezing the form in the meanwhile. </p>
  
  <p>In the current program I'm trying to write to a textbox, and actually see constant progress, and the form can't be affected by the tasks of the other thread.</p>
  
  <p>What I have now is I can write to a textbox with a thread using invoke, but It only shows the result (Form freezes while thread is busy), and the form freezes.</p>
  
  <p>Form image:  </p>
  
  <p><img src="https://i.stack.imgur.com/ZCMfa.png" alt="enter image description here"></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Threading;
  
  namespace MultiThreading
  {
  public partial class MultiThreading : Form
  {
      public MultiThreading()
      {
          InitializeComponent();
      }
  
      Thread writeOne, writeTwo;
  
      private void writeText(TextBox textBox, string text)
      {
          if (textBox.InvokeRequired)
          {
              textBox.BeginInvoke((MethodInvoker)delegate()
              {
                  for (int i = 0; i &lt; 500; i++)
                  {
                      textBox.Text += text;
                  }
              });
          }
          else
          {
              for (int i = 0; i &lt; 500; i++)
              {
                  textBox.Text += text;
              }
          }
      }
      private void btnWrite1_Click(object sender, EventArgs e)
      {
          writeOne = new Thread(() =&gt; writeText(txtOutput1, "0"));
          writeOne.Start();
      }
  
      private void btnWrite2_Click(object sender, EventArgs e)
      {
          writeTwo = new Thread(() =&gt; writeText(txtOutput2, "1"));
          writeTwo.Start();
      }
  
      private void btnClear1_Click(object sender, EventArgs e)
      {
          txtOutput1.Clear();
      }
  
      private void btnClear2_Click(object sender, EventArgs e)
      {
          txtOutput2.Clear();
      }
  
      private void btnWriteBoth_Click(object sender, EventArgs e)
      {
          writeOne = new Thread(() =&gt; writeText(txtOutput1, "0"));
          writeTwo = new Thread(() =&gt; writeText(txtOutput2, "1"));
  
          writeOne.Start();
          writeTwo.Start();
      }
  
      private void btnClearBoth_Click(object sender, EventArgs e)
      {
          txtOutput1.Clear();
          txtOutput2.Clear();
      }
  }
  
  }
  </code></pre>
  
  <p>EDIT:  </p>
  
  <p>Btw for anyone wondering, I'm new to multithreading and I'm just trying to write a small program to understand the best way to do this. </p>
  
  <p>I understand that my previous invoke didn't realy help because I still wasn't giving the form a chance to update, so its getting there.</p>
  
  <p>Ok so running 1 thread like this works, but still running multiple threads together, won't update the form till after the thread is done.<br>
  I've added a thread.sleep() so I can try and clear while writing, to see if I can still use the form. </p>
  
  <p>When writing to 1 textbox I can still clear the screen while writing.<br>
  But once I use 2 threads, I can't use the form anymore till the thread completes, and gives the output.</p>
  
  <pre><code>private void writeText(TextBox textBox, string text)
      {
          for (int i = 0; i &lt; 500; i++)
          {
              Invoke(new MethodInvoker(() =&gt;
              {
                  textBox.Text += text;
                  Thread.Sleep(2);
              }));
          }
  
      }
  </code></pre>
  
  <p>(If I'm totally wrong on this I don't mind having to read through some examples/threads, I'm still trying to see what is the best way to do this, besides a backgroundworker)</p>
  
  <p>EDIT 2: </p>
  
  <p>I've reduced the number of invokes by reducing the amount to write, but to increase delay giving the same effect of constant writing, just reducing the load.</p>
  
  <pre><code>private void writeText(TextBox textBox, string text)
      {
          for (int i = 0; i &lt; 500; i++)
          {
              Invoke(new MethodInvoker(() =&gt;
              {
                  textBox.Text += text;
                  Thread.Sleep(2);
              }));
          }
  
      }
  </code></pre>
  
  <p>EDIT 3:  </p>
  
  <p>Sumeet's example works using </p>
  
  <blockquote>
    <p>Application.DoEvents();</p>
  </blockquote>
  
  <p>(notice the s, .DoEvent doesn't work, typo probably :P), writing multiple strings simultaneously &amp; having them show the progress and not just the result.</p>
  
  <p>So Code update again :)</p>
  
  <p>*Using a new button to create 5 threads that write a random number to both textboxes</p>
  
  <pre><code>private void writeText(TextBox textBox, string text)
      {
          for (int i = 0; i &lt; 57; i++)
          {
              Invoke(new MethodInvoker(() =&gt;
              {
                  textBox.Text += text;
                  Thread.Sleep(5);
                  Application.DoEvents();
              }));
          }
  
      }
  private void btnNewThread_Click(object sender, EventArgs e)
      {
          Random random = new Random();
          int[] randomNumber = new int[5];
          for (int i = 0; i &lt; 5; i++)
          {
              randomNumber[i] = random.Next(2, 9);
              new Thread(() =&gt; writeText(txtOutput1, randomNumber[i-1].ToString())).Start();
              new Thread(() =&gt; writeText(txtOutput2, randomNumber[i-1].ToString())).Start();
          }
      }
  </code></pre>