Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
Application.DoEvents()


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??
Posted

1 solution

Yes, and not just this. Never use DoEvents 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 though DoEvents 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
 
Share this answer
 
Comments
Espen Harlinn 15-Mar-12 19:02pm    
A heartfelt 5!
Sergey Alexandrovich Kryukov 15-Mar-12 19:23pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900