Need help with canceling a routine

robplatt

Freshman
Joined
Jul 14, 2006
Messages
39
I have a sub that connects to a remote database to download records, could be 10 could be 10,000. I have a progress bar that shows the progress on which record its on, with a text to display a number 5/239. I have a "me.refresh" to keep the lable updating between records.

I have a cancel button on the form that when pressed, should set blnCancel to True. While my other sub is looping through the records, if it sees blnCancel = True then it should " throw new exception("Aborted") ". Maybe theres a better way then how im approaching this.

However. my problem lies in clicking the button. I can't click it. my app is too busy to listen for the click event. What should I do?
 
It sounds like you need to seperate the DB call out into a worker thread. The BackgroundWorker for .Net 2.0 is an easy way to do that. The catch is that you'll need to put a check in your method that downloads the records to keep an eye out for a cancel so that it knows when to terminate the thread (you can only politely request a background worker thread terminate.)

Check this thread out for an example: http://www.xtremedotnettalk.com/showthread.php?t=96990&highlight=backgroundworker
 
Thanks mskeel. I'm already using a few threads in my app with success. My only concern is being able to update the UI while the background worker is running. I'm found a difficult way to get around that in another area of my app, which can't apply to this scenerio. I will try the application.doevents. it sounds like it will work.
 
wow. application.doevents works great. its even allowing my controls to update faster then the me.refresh does. SWEET. thanks!
 
Ahh....I get what you are saying. I was thinking frozen UI which is not the case. Glad to see you got if figured out.
 
Back
Top