4

I have WPF application with a combobox filled with 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 a SSRS report. Everything works fine but the method takes a long time to complete, mostly because of the report, which means my UI becomes unresponsive. I tried a couple of ways to start a new thread/task but all of them are blocking the UI's thread. I'm probably doing something wrong but I have no idea.

What's the best way to call a long method in order to not block the UI?

EDIT

I changed my code to isolate the time-consuming part.

reportViewer.SetPageSettings(reportConfiguration.PageSettings); 

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

@LuisQuijada: That worked, post an answer so I can accept it.

5
  • 3
    can you show what your current code is doing (starting a new thread?) Commented Feb 7, 2013 at 21:54
  • did you try replace your call with async call ? Commented Feb 7, 2013 at 21:58
  • Use background-workers. See this other SO thread Commented Feb 7, 2013 at 21:58
  • @LuisQuijada That worked, post an answer so I can accept it. Commented Feb 8, 2013 at 14:04
  • @fhlamarche glad that it worked! Unfortunately I cannot post anything here... Commented Feb 8, 2013 at 14:19

2 Answers 2

11
using System.Threading; new Thread(() => { Thread.CurrentThread.IsBackground = true; /* run your code here */ Console.WriteLine("Hello, world"); }).Start(); 
Sign up to request clarification or add additional context in comments.

5 Comments

Alright let me try that.
That's some ugly code ...
Nope, it gives me "The calling thread must be STA, because many UI components require this", like a couple of methods I tried.
@fhlamarche - if you are calling UI elements from the code you want to put in the thread then you'll get this error message. Look into using events and dispatchers to marshal UI calls from background worker threads.
@ChrisF My method doesn't use any of it's parent's members. It instantiate a new windows with a reportViewer and just calls .Show()
1

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

As a start place you may look at suggested link in your post and/or the MSDN article:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.