Question: 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).

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?

Eg. I have a call through my `FrontController`, that takes time:

```
_controller.testExportExcel(wrapper, saveDialog.FileName);

```

Since it's creating an Excel file. I won't the app to be responding on the UI while its working.

Another example of a process heavy task could be this:

```
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 && (bool)r.Cells["News"].Value == true)
         r.DefaultCellStyle = red;
     }
  }
}

```

Where the `foreach` 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??

Answer: 
How about using a [Task](http://msdn.microsoft.com/en-us/library/dd321439.aspx) (if targetting .net 4)? This is considered as a replacement of the BackgroundWorker class since it supports nesting (parent/child tasks), task continuations, etc.

E.g. 

```
    private void dataGridView_liste_DataBindingComplete(object sender,
      DataGridViewBindingCompleteEventArgs e)  
    {
        Task t = Task.Factory.StartNew(() =>
        {
            // do your processing here - remember to call Invoke or BeginInvoke if
            // calling a UI object.
        });
        t.ContinueWith((Success) =>
        {
            // callback when task is complete.
        }, TaskContinuationOptions.NotOnFaulted);
        t.ContinueWith((Fail) =>
        {
            //log the exception i.e.: Fail.Exception.InnerException);
        }, TaskContinuationOptions.OnlyOnFaulted);

    }

```
For your first example, a call to `_controller.testExportExcel()`, a `BackgroundWorker` or `Task Parallel Library` call (i.e. `Task.Factory.StartNew(...)`) would be appropriate to satify your requirement of keeping the UI responsive. Plenty of examples floating around, including the other answers here.

For your second example, you will find you can't put this on a background thread since it appears to be code that manipulates the UI. Specifically, if the implementation of your `BackgroundWorker`'s `DoWork` event handler, or the delegate you pass to `Task.Factory.StartNew()`, or the method for a plain old thread touch the UI, you are highly likely (/certain?) to get an exception stating "Cross-thread operation not valid". 

The reason for this is covered [in this question](https://stackoverflow.com/questions/8738767/backgroundworker-cross-thread-operation-not-valid). But I'm more surprised actually this is slow enough that you want to make it asynchronous. There might be some simple ways to make this code more responsive - `Control.SuspendLayout()` and `.ResumeLayout()` springs to mind.