Window doesnt load during processing

atmosphere

Member
Joined
Jun 14, 2005
Messages
8
Programming Experience
5-10
Hey

I have a button which triggers a non-terminating loop. The only way to exit this loop is to click the button again. I find that the application becomes unresponsive when this loop is running so i cant click the button to stop it.

Is there a way to get past this?
 
Let me make a few comments. Although I haven't read it in detail, the thread that kulrom mentions suggests using Application.DoEvents(). In your situation, clicking the button raises a Click event but the app never gets to handle that event because it is too busy performing the operations within your loop. Calling Application.DoEvents() within your loop causes the app to handle any events that are pending at that point, which may include your button's Click event. At that point, the Click event handler of your button will be called and the loop can be exited. One of the drawbacks of this method is that you need to make sure that the time between calls to Application.DoEvents() is not too great, otherwise the lag between clicking the button and the UI indicating that it has been clicked may be great. This can be annoying and confusing to the user, possibly prompting them to press the button repeatedly.

The following may be too complex for you right now but it is good to know anyway. The "proper" way to achieve what you want is to start a new thread and perform the loop in that thread. Your UI will remain responsive so your button click will be handled (almost) immediately and then you can terminate the thread that is doing the work. When I say "terminate", it can be done gracefully or forcefully, depending on the situation.
 
Back
Top