title: C# Threading still make UI unresponsive
body: <p>Well , I have a form1 which has buttons and if you click one of its button 
  It would load the UserControl into panel in form1</p>
  
  <p>That usercontrol1 contains a lot of data like Database,charts and picture boxes too. So it would definitely make the User Interface unresponsive while loading.</p>
  
  <p>So I read some article and I found out that I need to run it through another thread so I tried it and it just increase the performance by a little bit.</p>
  
  <p>The usercontrol1 still make the GUI unresponsive for about 3-5 sec and what if my data become larger.</p>
  
  <p>I want to make it responsive and show to user that still loading by running the animated picturebox and stop if its finish loading</p>
  
  <p>here is my code:</p>
  
  <pre><code>private void click_dashb_Click(object sender, EventArgs e)
      {
  
          ParameterizedThreadStart pts = new ParameterizedThreadStart(load_UserControl);
          Thread t = new Thread(pts);
          t.Start();
  
          //Animated Picturebox to show user that UI is loading
          pictureBox1.Enabled = true;
  
  
          hover.Location = new Point(42, 130);
      }
  
  
      private void load_UserControl(object state)
      {
  
          Invoke(new Action(() =&gt;
          {
              //load user control through another thread
              while (panel1.Controls.Count &gt; 0)
                  panel1.Controls[0].Dispose();
  
  
              Home frm = new Home();
  
  
              frm.AutoScroll = true;
              panel1.Controls.Add(frm);
              frm.Show();
          }));
  
          //Stop the animated GIF means the load is finish!
          pictureBox1.Enabled = false;
      }
  </code></pre>
  
  <p>If you help me about this problem. I might apply it to all of my works. because most of it contains large data.</p>
  
  <p>Thanks stackoverflow community :)</p>
  
  <p>EDIT:</p>
  
  <p>After reading the comments suggesting to use Background worker . I tried to use it. but still getting a little bit unresponsiveness 
  Here's the new code:</p>
  
  <pre><code> private void click_dashb_Click(object sender, EventArgs e)
      {
  
          bgw.RunWorkerAsync();
  
      }
   private void bgw_DoWork(object sender, DoWorkEventArgs e)
      {
  
          try
          {
  
  
              BeginInvoke((MethodInvoker)delegate
              {
                  while (panel1.Controls.Count &gt; 0)
                      panel1.Controls[0].Dispose();
  
  
                  Home frm = new Home();
  
  
                  frm.AutoScroll = true;
                  panel1.Controls.Add(frm);
                  frm.Show();
              });
  
  
          }
          catch (Exception x)
          {
              MessageBox.Show("An error occured while performing operation" + x);
          }
  
      }
  
      private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
  
      }
  
      private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          if (e.Cancelled)
          {
              MessageBox.Show("Operation Cancelled");
  
          }
          else
          {
  
              MessageBox.Show("Operation Completed");
  
  
          }
      }
  </code></pre>
  
  <p>It's a little bit better but i still got a little unresponsiveness. Can you check out my code and what's wrong with this.? </p>
  
  <p>thanks again</p>