title: Slow UI rendering in C# Winforms
body: <p>I'm making a relatively rudimentary <code>WinForms</code> user control with a number of buttons, each representing a table from a database.</p>
  
  <p>The data from each table is loaded into a corresponding <code>DataGridView</code> after the <code>InitializeComponent</code> method is called.</p>
  
  <p>When one of the aforementioned buttons is clicked, its corresponding <code>DataGridView</code>, and those representing tables linked to the main one through FKs become visible and then a <code>BackgroundWorker</code> gradually increases them to their desired sizes and shifts controls to appropriate locations over a short period of time.</p>
  
  <pre><code>public uc1()
  {
      InitializeComponent();
  
      PopulateTables();
  
      tableResizer.WorkerReportsProgress = true;
      tableResizer.WorkerSupportsCancellation = true;
      tableResizer.DoWork += tableResizer_DoWork;
      tableResizer.ProgressChanged += tableResizer_ProgressChanged;
      tableResizer.RunWorkerCompleted += tableResizer_RunWorkerCompleted;
  
      dgv5.SelectionChanged += show5Relations;
  }
  
  private void btn5_Click(object sender, EventArgs e)
  {
      mainTable = 5;
      btn5.Visible = false;
      btn4.Visible = false;
      btn3.Visible = false;
      btn2.Visible = false;
      dgv5.Visible = true;
      dgv4.Visible = true;
      dgv3.Visible = true;
      dgv2.Visible = true;
      lbl6.Visible = false;
      lbl7.Visible = false;
      lbl1.Visible = false;
      tableResizer.RunWorkerAsync();
  }
  
  private void tableResizer_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  {
      int expansion = 0;
      while (expansion &lt; 30)
      {
          tableResizer.ReportProgress(mainTable);
          expansion++;
          System.Threading.Thread.Sleep(16);
      }
  }
  
  private void tableResizer_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
  {
      if (e.ProgressPercentage == 5)
      {
          dgv5.Size = new Size(dgv5.Size.Width + (((this.Width / 26) * 10) / 30), dgv5.Size.Height + (((this.Height / 24) * 10) / 30));
          dgv5.Location = new Point(dgv5.Location.X - (((this.Width / 26) * 10) / 60), dgv5.Location.Y - (((this.Height / 24) * 10) / 60));
  
          dgv4.Size = new Size(dgv4.Size.Width + (((this.Width / 26) * 10) / 30), dgv4.Size.Height + (((this.Height / 24) * 10) / 50));
          dgv4.Location = new Point(dgv4.Location.X - (((this.Width / 26) * 10) / 40), dgv4.Location.Y - (((this.Height / 24) * 10) / 120));
  
          dgv3.Size = new Size(dgv3.Size.Width + (((this.Width / 26) * 10) / 30), dgv3.Size.Height + (((this.Height / 24) * 10) / 50));
          dgv3.Location = new Point(dgv3.Location.X - (((this.Width / 26) * 10) / 40), dgv3.Location.Y - (((this.Height / 24) * 10) / 120));
  
          dgv2.Size = new Size(dgv2.Size.Width + (((this.Width / 26) * 10) / 30), dgv2.Size.Height + (((this.Height / 24) * 10) / 50));
          dgv2.Location = new Point(dgv2.Location.X - (((this.Width / 26) * 10) / 40), dgv2.Location.Y - (((this.Height / 24) * 10) / 120));
  
          btn1.Location = new Point(btn1.Location.X - (((this.Width / 26) * 10) / 130), btn1.Location.Y);
      }
  }
  
  private void tableResizer_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  {
      if (mainTable == 5)
      {
          btn7.Visible = true;
          btn6.Visible = true;
          btn1.Visible = true;
          lbl5.Visible = true;
          lbl5.Location = new Point(dgv5.Location.X, dgv5.Location.Y - lbl5.Size.Height);
          lbl4.Visible = true;
          lbl4.Location = new Point(dgv4.Location.X, dgv4.Location.Y - lbl4.Size.Height);
          lbl3.Visible = true;
          lbl3.Location = new Point(dgv3.Location.X, dgv3.Location.Y - lbl3.Size.Height);
          lbl2.Visible = true;
          lbl2.Location = new Point(dgv2.Location.X, dgv2.Location.Y - lbl1.Size.Height);
      }
  }
  </code></pre>
  
  <p>The problem is that the UI is not being rendered fast enough when the <code>tableResizer</code> is doing its work, and as a result, one of the <code>DataGridViews</code> is not fully visible until the work is finished.</p>
  
  <p>Increasing the length of the Sleep in the <code>DoWork</code> method only fixes this issue when it is at 60ms, at which point the rendering is too choppy to be acceptable. Anything more than 30ms will not do.</p>
  
  <p>I am relatively new to multithreading and arguably C# in general, so I am pretty sure there are more efficient ways to go about this.</p>
  
  <p>What should I do to ensure smooth rendering?</p>