robplatt Posted August 31, 2006 Posted August 31, 2006 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? Quote
Cags Posted August 31, 2006 Posted August 31, 2006 Adding Application.DoEvents() to the loop should allow the UI to still be used. Quote Anybody looking for a graduate programmer (Midlands, England)?
mskeel Posted August 31, 2006 Posted August 31, 2006 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 Quote
robplatt Posted August 31, 2006 Author Posted August 31, 2006 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. Quote
robplatt Posted August 31, 2006 Author Posted August 31, 2006 wow. application.doevents works great. its even allowing my controls to update faster then the me.refresh does. SWEET. thanks! Quote
mskeel Posted August 31, 2006 Posted August 31, 2006 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.