首页 > 解决方案 > 应用程序工作并冻结时更改表单

问题描述

我正在开发一个 C# WinForms 应用程序;此应用程序将有许多窗口来显示一些报告。这些窗口是一些 DLL 通过反射创建的窗体,因此代码不是那么容易更改。

问题是当一个表单正在获取数据或处理一些信息时,它会冻结/锁定所有应用程序,因此用户无法导航到应用程序中的其他表单。因此,如果用户想要一个需要 5 分钟的报告,则应用程序会冻结 5 分钟,并且用户无法导航到另一个窗口。

我试图用 Invoke((MethodInvoker)delegate () {}); 但无论如何它都会冻结。我尝试使用 Windows 窗体中的 MDI,但也冻结了。我尝试使用 DevExpress 的 Document Manager 或 Tab View,但同样的问题。

我也已经开始阅读问题,以使用主线程的 UI 在新线程中创建表单,但我认为这不是我的问题,因为冻结表单是可以的,但不能冻结所有应用程序和用户可以浏览其他表格。

如果我尝试在其他进程中创建表单,应用程序加载速度太慢,因为它拥有的所有控件,所以理想情况下它必须在同一个进程中。

有什么方法可以在新线程中调用新表单,在处理时不会冻结所有应用程序?是否有任何开源/付费控件可以帮助我解决这个问题?

预先感谢,问候。

标签: c#multithreadingwinformsfreeze

解决方案


在 winforms 和 C# 中进行异步编程的最简单方法是异步/等待逻辑。

尝试这样的事情:

using System.Threading;
using System.Threading.Tasks;

private async void btnDoReport_Click(object sender, EventArgs e)
{
    Task<bool> reportTask = new Task<bool>(ReportFunction); // if your function is void you can create it as Task reportTask = new Task(ReportFunction)
    trainTask.Start();  // here your task - report is executed asynchronously and GUI does not freeze

    bool reportResult = await  reportTask; // wait on resposne from your reportTask
}

private bool ReportFunction()
{
  // do what ever you need to in this function
  return true;
}

您还可以使用同样异步工作的BackgroundWorker 类,甚至可以从异步运行的线程中引发事件。之后,您可以在主线程 - GUI 线程中捕获此事件,因此您将能够安全地访问您的 GUI 应用程序的控件而不会出错。

 using System.ComponentModel;

 private BackgroundWorker bWork = new BackgroundWorker();
 this.bWork.DoWork += BWork_DoWork;
 this.bWork.ProgressChanged += bwork_ProgressChanged;

 private void btnReport_Click(object sender, EventArgs e)
 {
     // this will run your code asynchronously
     this.bWork.RunWorkerAsync();
 }

 private void BWork_DoWork(object sender, DoWorkEventArgs e)
 {
     // here you can create your report

    // if you want you can raise event like this :
     this.bWork.ReportProgress(this)
    // and after raising of above event function : bWork_ProgressChanged is 
    //called where you can safely access main thread controls
 }

                   
 private void bwork_ProgressChanged(object sender, 
 System.ComponentModel.ProgressChangedEventArgs e)
 {
   /* here you can access main thread GUI elements for example progress bar 
   without worrying of some problems because this function is called in 
   main thread space*/
 }

推荐阅读