Application.DoEvents(),并冻结了一些机器上结了、机器上、Application、DoEvents

2023-09-06 15:47:32 作者:陪你死i

我有一个WinForms应用程序,它会调用Application.DoEvents()。现在,应用程序死机解决此方法被调用时(毫不奇怪)。

I have a winforms application which makes calls to Application.DoEvents(). Now the app freezes around the time this method is called (no surprise).

不过,这只是发生在一些机器上。它是公平地说,这将是由于机器本身还是仍然下跌到code / application.doevents()?在这种情况下,它应该被重构以使用的BackgroundWorker

However, this only happens on some machines. Is it fair to say that this would be due to machine itself or is it still down to the code/application.doevents()? In which case, it should be refactored to use backgroundworker.

这将是一个机器/环境问题或奥珀蒂尼蒂使用的BackgroundWorker?

Would this be a machine/environment issue or an oppurtunity to use backgroundworker?

推荐答案

如果你可以使用背景工人。 的DoEvents 是黑客,让您的应用程序保持响应和处理WM_PAINT消息主要是,当它做一些事情的它实际上应该永远做的:阻断执行以上在主GUI循环消息处理期间在合理的时间。如果的DoEvents 引起挂起,而不是修复它,你有太多的信息花费了太多的系统电源。 有些机器变成最机器,否则

Use a background worker if you can. DoEvents is a 'hack' to allow your application to remain responsive, and process WM_PAINT messages mostly, when it's doing something it should actually never be doing: blocking execution for more than a reasonable time during message processing in the main GUI loop. If DoEvents causes a hang instead of fixing it, you have too many messages spending way too much system power. Fork them out to background workers to fix all of it at once - it's just a matter of time before "some machines" becomes "most machines" otherwise.