【发布时间】:2016-01-07 15:57:24
【问题描述】:
我的问题是如何在 UI 线程忙于查询数据库时从另一个线程更新 UI 控件(例如 ProgressBar)(运行耗时的存储过程)?
我检查了这些问题,但仍然没有找到适合我的解决方案。
- Updating UI continuously while the main thread is busy
- How to update the GUI from another thread in C#?
- Run multiple UI Threads
- Cross-thread cross-form. Display a splash screen with a progress bar
我知道一种选择是使用 BackgroundWorker 进行耗时的工作,并使用 更新 ProgressBar >ReportProgress 方法,但是我在这个选项中有一个问题,因为 UI 线程负责实例化并在查询数据库后显示另一个表单,如下所示:
Form2 f2=new Form2();
f2.show();
【问题讨论】:
-
我不明白为什么你不能在辅助线程上做任何耗时的事情,然后在主线程上打开表单。也许你应该在主线程忙时(如果可能的话)强制刷新 UI 之前考虑这一点。
-
你刚刚指出了你的缺陷。永远不要从主 UI 线程查询数据库!
标签: c# multithreading winforms