How to call a long method in a new thread to support user interface in C #

I have a WPF application with fields populated by users, a grid showing some data for the selected user, and a button that calls DoTimeSheetReport ().

DoTimeSheetReport () does some work and then opens a new window with the SSRS message. Everything works fine, but it takes a lot of time, mainly because of the report, which means my user interface is becoming unresponsive. I tried several ways to start a new thread / task, but they all block the UI thread. I'm probably doing something wrong, but I have no idea.

What is the best way to call a long method so as not to block the user interface?

@Kyle Here is the last working (but blocking) method I tried

var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(() => { }) .ContinueWith(r => DoTimeSheetReport(), scheduler); 

EDIT

I modified my code to isolate the laborious part.

 reportViewer.SetPageSettings(reportConfiguration.PageSettings); 

Using backgroundWorker in this part did it. Thank you for your help.

@LuisQuijada: It worked, send an answer so that I can accept it.

+4
source share
2 answers
 using System.Threading; new Thread(() => { Thread.CurrentThread.IsBackground = true; /* run your code here */ Console.WriteLine("Hello, world"); }).Start(); 
+6
source

In short: what you need to do is look at how to use asynchronous calls .

As a starting place, you can see the proposed link in your message and / or in the MSDN article:

+2
source

All Articles