如何在新线程中调用long方法以保持UI在C#中运行

0xF*_*xFF 4 c# wpf user-interface multithreading reporting-services

我有一个带有组合框的WPF应用程序,一个显示所选用户的一些数据的网格和一个调用DoTimeSheetReport()的按钮.

DoTimeSheetReport()执行一些工作,然后打开一个带有SSRS报告的新窗口.一切正常,但该方法需要很长时间才能完成,主要是因为报告,这意味着我的UI变得没有响应.我尝试了几种方法来启动一个新的线程/任务,但所有这些都阻止了UI的线程.我可能做错了但我不知道.

为了不阻止UI,调用long方法的最佳方法是什么?

编辑

我更改了代码以隔离耗时的部分.

reportViewer.SetPageSettings(reportConfiguration.PageSettings);
Run Code Online (Sandbox Code Playgroud)

在这部分上使用backgroundWorker就可以了.谢谢您的帮助.

@LuisQuijada:这很有效,发布了答案,所以我可以接受.

Pri*_*kar 10

using System.Threading;
new Thread(() => 
{
    Thread.CurrentThread.IsBackground = true; 
    /* run your code here */ 
    Console.WriteLine("Hello, world"); 
}).Start();
Run Code Online (Sandbox Code Playgroud)