WinForms 应用程序似乎“没有响应”从数据库获取数据时?

发布于 2024-10-15 01:33:00 字数 133 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-10-22 01:33:00

您可以使用 BackgroundWorker 来减轻对后台线程以避免阻塞主 UI 线程。

You could use a BackgroundWorker to offload lengthy operations on a background thread to avoid blocking the main UI thread.

像极了他 2024-10-22 01:33:00

所有 Windows 应用程序都是这样的,如果您从事件中执行任何冗长的操作,您将在一段时间内“无响应”,因为程序不响应系统的其余部分。这里有几个选项:

  • Application.DoEvents(如果您有一个包含许多离散快速操作的循环),并且每次操作后的 DoEvents
  • 通过 Threads 或 BackgroundWorker 将任务推入后台。

如果您要使用第二个选项,请确保不要直接更新 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:

  • Application.DoEvents, if you have a loop with many discrete fast operations, and DoEvents after each operation
  • push the task into background, via Threads or BackgroundWorker.

If you will use second options, make sure that you don't update anything on UI directly, because it will break, use Invoke() instead.

江心雾 2024-10-22 01:33:00

如果您使用单一接口,那么最好使用Application.DoEvents()。如果您有一个表单在单击按钮时执行一系列任务,那么您正在使用单个界面。

为什么使用 Application.DoEvents()?

来自 DoEvents() 的 MSDN 页面

当您运行 Windows 窗体时,它会创建新窗体,然后等待事件进行处理。每次表单处理一个事件时,它都会处理与该事件关联的所有代码。所有其他事件都在队列中等待。当您的代码处理该事件时,您的应用程序不会响应。例如,如果将另一个窗口拖到顶部,则该窗口不会重新绘制。

如果您在代码中调用 DoEvents,您的应用程序就可以处理其他事件。例如,如果您有一个将数据添加到 ListBox 并将 DoEvents 添加到您的代码的表单,则当将另一个窗口拖动到该表单上时,您的表单将重新绘制。如果从代码中删除 DoEvents,则在按钮的单击事件处理程序完成执行之前,您的表单将不会重新绘制。有关消息传递的详细信息,请参阅 Windows 窗体中的用户输入。

简而言之,如果您单击按钮并且必须等待某些工作完成,Application.DoEvents() 将刷新表单 UI 并消除(不响应)问题。

如何使用DoEvents()
同一 MSDN 页面还指出

通常,您在循环中使用此方法来处理消息。

因此,判断它应该去哪里的最简单方法是在程序中查找发生冗长操作的循环,并将 Application.DoEvents() 放置在该循环内。我通常将它放在所述循环内的末尾。

例如:

foreach (var foo in bar)
  {
    // Do work
    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():

When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.

If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing. For more information on messaging, see User Input in Windows Forms.

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

Typically, you use this method in a loop to process messages.

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:

foreach (var foo in bar)
  {
    // Do work
    Application.DoEvents();
  }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文