可能的危险Application.DoEvents [英] Possible Dangers Application.DoEvents

查看:112
本文介绍了可能的危险Application.DoEvents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在VB6中使用DoEvents来释放多任务并完成工作(例如comms). C#等效于Application.DoEvents,我想知道它是否被视为多核PC(以及Thread.Sleep())的不良做法.由于许多年前我在Windows 2000中仅使用VB6,所以我在网上搜索并发现了一些冲突的信息.

谢谢
Glenn

Hi,

I have used DoEvents in VB6 to free up multi-tasking and get stuff (like comms) done. The C# equlivalent is Application.DoEvents, I would like to know if it is viewed as bad practice with multi-core PC''s (along with Thread.Sleep()). As I have only used VB6 many moons ago in Windows 2000. I have searched the web and found some conflicting info.

Thanks
Glenn

推荐答案

是.

如果您的应用程序需要DoEvents,则它的设计不正确-您不应该在UI线程中做任何耗时的事情(并且这既适用于VB代码,也适用于PC).您应该将该代码移到单独的线程上-使用 BackgroundWorker类 [^ ].非常简单,链接中包含一个示例.
Yes.

If your application needs DoEvents, then it is badly designed - you should not be doing anything in your UI thread which takes significant amounts of time (and this applies to your VB code as well as PC). You should move that code onto a separate Thread - look at using a BackgroundWorker class[^]. It''s pretty simple, and the link contains an example.




Jeff Atwood 的几篇不错的帖子将清除您的冲突
Hi,

Couple of nice post by Jeff Atwood will clear your confliction


您唯一应该使用DoEvents的情况是您是否想要处于紧密的处理循环中,并且实际上是自己管理消息队列.如果您正在编写互动游戏,那么这可能是合法的.通常,对于普通应用程序,您应该使用线程.
The only time you should be using DoEvents is if you want to be in a tight processing loop and essentially managing the message queue yourself. If you''re writing an interactive game this can be legitimate. Generally, for normal applications, you should thread.


这篇关于可能的危险Application.DoEvents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆