title: Run task asynchronously in C#
body: <p>I have some process heavy tasks that run in my WinForms app. The problem is, while its running, it freeze the UI (UI main thread).</p>
  
  <p>I haven't worked that much with threads and delegates in C# yet, and that's why I hope someone could help me to, how to handle those process heavy tasks, without freezing the UI, so the user don't think the app is crashing while waiting?</p>
  
  <p>Eg. I have a call through my <code>FrontController</code>, that takes time:</p>
  
  <pre><code>_controller.testExportExcel(wrapper, saveDialog.FileName);
  </code></pre>
  
  <p>Since it's creating an Excel file. I won't the app to be responding on the UI while its working.</p>
  
  <p>Another example of a process heavy task could be this:</p>
  
  <pre><code>private void dataGridView_liste_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
  {
    if (e.ListChangedType != ListChangedType.ItemDeleted)
    {
       foreach (DataGridViewRow r in dataGridView_liste.Rows)
       {
         DataGridViewCellStyle red = dataGridView_liste.DefaultCellStyle.Clone();
         red.BackColor = Color.LightGreen;
         if (r.Cells["News"].Value != null &amp;&amp; (bool)r.Cells["News"].Value == true)
           r.DefaultCellStyle = red;
       }
    }
  }
  </code></pre>
  
  <p>Where the <code>foreach</code> loop takes time, and freeze the UI. An async thread running the process and automatically closing when its done, could be useful I think. But how does it work??</p>