WinForms 应用程序似乎“没有响应”从数据库获取数据时?
我在 WinForms 应用程序 (VB.NET) 上工作,它处理 CRUD 操作。 当它加载数据时,似乎出现“无响应”状态,加载完成后,一切都会正常。
当你的表单从数据库中获取记录时,你如何解决类似的问题?
非常感谢。
I work on WinForms app (VB.NET) which handles CRUD operations.
When it loads the data, it seems state like "Not Responding" and when loading finished, every thing will be normal.
How do you solve things like it when your forms fetch records from database?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 BackgroundWorker 来减轻对后台线程以避免阻塞主 UI 线程。
You could use a BackgroundWorker to offload lengthy operations on a background thread to avoid blocking the main UI thread.
所有 Windows 应用程序都是这样的,如果您从事件中执行任何冗长的操作,您将在一段时间内“无响应”,因为程序不响应系统的其余部分。这里有几个选项:
如果您要使用第二个选项,请确保不要直接更新 UI 上的任何内容,因为它会破坏,请改用 Invoke()。
All windows apps are such that if you execute any lenghty operation from your events you'll get 'not responding' for some period of time, because program isn't responding to the rest of the system. You have several options here:
If you will use second options, make sure that you don't update anything on UI directly, because it will break, use Invoke() instead.
如果您使用单一接口,那么最好使用
Application.DoEvents()
。如果您有一个表单在单击按钮时执行一系列任务,那么您正在使用单个界面。为什么使用 Application.DoEvents()?
来自 DoEvents() 的 MSDN 页面:
简而言之,如果您单击按钮并且必须等待某些工作完成,
Application.DoEvents()
将刷新表单 UI 并消除(不响应)问题。如何使用DoEvents()
同一 MSDN 页面还指出
因此,判断它应该去哪里的最简单方法是在程序中查找发生冗长操作的循环,并将
Application.DoEvents()
放置在该循环内。我通常将它放在所述循环内的末尾。例如:
为什么单个接口不使用BackgroundWorker?
当您在不需要多线程的地方创建多个线程时,就会留下线程泄漏的可能性。例如,如果您实现了一个BackgroundWorker来处理CRUD操作,但随后由于某种原因关闭了程序,则界面将关闭,但BackgroundWorker线程将继续运行。您可以通过任务管理器确认该进程仍在运行。
当您有其他设备(例如打印机)在使用时,BackgroundWorkers 是理想的选择。也就是说,您可能不希望程序在完成文档打印之前停止;因此,当您继续使用程序时,新线程可以处理打印作业。
If you are using a single interface, it is ideal to use
Application.DoEvents()
. If you have one form that does a sequence of tasks when you click a button, then you're using a single interface.Why Application.DoEvents()?
From the MSDN page on DoEvents():
In a nutshell, if you click a button and have to wait for some work to be done,
Application.DoEvents()
will refresh the form UI and get rid of the (not responding) issue.How to use DoEvents()
The same MSDN page also states that
So, the easiest way to tell where it should go is to look for a loop in your program where a lengthy operation occurs and place
Application.DoEvents()
inside that loop. I typically place it right at the end inside said loop.For example:
Why Not BackgroundWorker for a Single Interface?
When you create multiple threads where multithreading is not needed, you leave the possibility of thread leak. For example, if you implement a BackgroundWorker to handle the CRUD operations but then, for some reason, close out the program, the interface will close but the backgroundworker thread will keep running. You can confirm that this process is still running via task manager.
BackgroundWorkers are ideal when you have other devices in play, such as printers. That is, you probably wouldn't want your program to halt until it's finished printing a document; for that reason, a new thread can handle the print job while you continue to use your program.