要使用.DoEvents()退出while循环,需要额外单击吗? [英] An extra click required to get out of a while loop using .DoEvents() ?
问题描述
大家好,我写了一个小API gui来通过基金会现场总线进行通信.
我使用了While循环,该循环不断从总线读取一些参数值.
在While循环的结尾,我放了
Hi folks, I wrote a small API gui to communicate over foundation fieldbus.
I''ve used a While loop that constantly reads some parameter values from the bus.
At the end of the While loop, I put
Application.DoEvents()
这样我可以使用另一个按钮停止阅读过程.一切似乎都很好,也很容易..但是有一个问题:当我运行While循环并希望停止该过程时,我必须先单击表单上的任意位置,然后才能单击任何按钮(大多是停止阅读"或重置"按钮).额外的点击使我的表单显得反应迟钝和la脚.
我想知道这是否是与DoEvents()有关的问题,还是我对Foundation现场总线的使用.
任何人都经历过这种额外点击"?
so that I can use another button to stop the reading process. It all seems good and easy.. but there''s one problem: when I get the While loop running and wish to stop the process, I have to first click somewhere on the form, anywhere, before I could click any buttons(either ''stop reading'' or ''reset'' button mostly). That extra click makes my form seem unresponsive and lame.
I wonder whether this is a problem pertaining to DoEvents() or my use of Foundation fieldbus.
Anyone experience this ''extra click'' before??
推荐答案
是的,不仅限于此.切勿将DoEvents
用于此类目的.对于具有自己的线性或圆形逻辑的任何事物,以及包含阻塞调用或冗长运算的任何事物,请始终使用单独的线程.即使DoEvents
可以作为一种快速而又肮脏的基于UI线程的解决方法,但它并没有那么快,但对于许多情况而言却太脏而无法正常工作.
如果将非UI线程与UI应用程序一起使用,则需要了解不能直接从此类线程调用任何UI方法或属性,需要使用我在过去的回答中详细介绍的调用机制.请参阅:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].
—SA
Yes, and not just this. Never useDoEvents
for such purposes. Always use a separate thread for anything which has it''s own linear or circular logic, and for anything which contains blocking calls or contain lengthy operation. Even thoughDoEvents
can work as a quick and dirty UI thread based work-around, it''s not so quick but is too dirty for many cases to work correctly.
If you use a non-UI thread with UI applications, you need to understand that you cannot call any UI methods or properties directly from such thread, you need to use invocation mechanism I describe in detail in my past answers. Please see:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].
Please also see more references on threading based on my past answers:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].
—SA
这篇关于要使用.DoEvents()退出while循环,需要额外单击吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!