.NET 中的 DoEvents

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/820471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:37:24  来源:igfitidea点击:

DoEvents in .NET

.netvb.net

提问by Jeff Yates

What's the equivalent of the VB6's DoEvents in .NET?

.NET 中 VB6 的 DoEvents 的等价物是什么?

EDIT:

编辑:

I have a Sub that takes a long time to do its work. (it has a do-while) when I call it, The form turns white. in VB6 I used to put a DoEvents in the method (inside its do-while) to prevent this.

我有一个需要很长时间才能完成工作的 Sub。(它有一个do-while)当我调用它时, 形式变成白色。在 VB6 中,我曾经在方法中(在它的 do-while 中)放置一个 DoEvents 来防止这种情况。

回答by Jon B

There are few (if any) cases in which DoEvents is the right solution in .NET. If you post a little about what you're doing, we might have some suggestions as to an alternative.

在 .NET 中 DoEvents 是正确的解决方案的情况很少(如果有的话)。如果您发布一些关于您正在做什么的信息,我们可能会提供一些替代方案的建议。



In response to your edit, what you need to do is create a BackgroundWorker. This will keep your main (GUI) thread free, allowing it to repaint and behave normally. There are many tutorials on the web for this, including this one.

为了响应您的编辑,您需要做的是创建一个BackgroundWorker。这将使您的主 (GUI) 线程保持空闲,允许它重新绘制并正常运行。网上有很多这方面的教程,包括这个

回答by Jeff Yates

Application.DoEvents()should be what you're looking for.

Application.DoEvents()应该是你要找的。

However, note the following (from the linked MSDN page):

但是,请注意以下内容(来自链接的 MSDN 页面):

Unlike Visual Basic 6.0, the DoEvents method does not call the Thread.Sleep method.

与 Visual Basic 6.0 不同,DoEvents 方法不调用 Thread.Sleep 方法。

Update

更新

Given that the questions now provides an explanation of usage, I would say refactoring to use a background thread would be a better solution. DoEvents can lead to some issues as it will cause the underlying message queue to pump, which can lead to re-entrancy and a whole host of other side-effects. DoEvents has valid use-cases and I would shy from saying that it's use is bad practise - it exists for valid reasons - but in most cases, there are better solutions than just calling DoEvents.

鉴于这些问题现在提供了用法的解释,我会说重构以使用后台线程将是更好的解决方案。DoEvents 可能会导致一些问题,因为它会导致底层消息队列抽水,从而导致重入和一系列其他副作用。DoEvents 有有效的用例,我不敢说它的使用是不好的做法——它存在是有正当理由的——但在大多数情况下,有比仅仅调用 DoEvents 更好的解决方案。

回答by dr. evil

Other answers are correct, aS a side note If you need to use DoEvents() consider using BackgroundWorker or Control.Invoke . 99% of the time DoEvents() indicates some dirty hack.

其他答案是正确的,作为旁注 如果您需要使用 DoEvents() 考虑使用 BackgroundWorker 或 Control.Invoke 。99% 的时间 DoEvents() 表示一些肮脏的黑客。

回答by arif

simplest way is :

最简单的方法是:

Application.DoEvents()

Application.DoEvents()

Thus you can call DoEvents

这样你就可以打电话 DoEvents

回答by DoEventsIsJustDifferentNow

For those who think DoEvents is evil just use this instead:

对于那些认为 DoEvents 是邪恶的人,请改用它:

System.Windows.Forms.Application.ThreadContext.FromCurrent.RunMessageLoop(2, Nothing)

System.Windows.Forms.Application.ThreadContext.FromCurrent.RunMessageLoop(2, Nothing)