Click to See Complete Forum and Search --> : creating a cancel button to exit my loops..
effekt26
Jul 13th, 2007, 12:26 AM
Good Afternoon,
What im trying to do is create a cancel button for my application. Basically, my application selects data out of a database, and inserts into notes...however there are alot of loops and different subroutine calls to make it do what it is intended to do...
what i need to know is, is there anyway that i can exit all of the loops and subroutine, when the user clicks the cancel button, and just go back to how it was when the program loaded...but keeping any text in the textbox, as this has output in it?
Cheers,
Justin
jmcilhinney
Jul 13th, 2007, 12:30 AM
Use a BackgroundWorker to perform the loop. You can cancel a BackgroundWorker's task quite easily.
effekt26
Jul 13th, 2007, 01:07 AM
Use a BackgroundWorker to perform the loop. You can cancel a BackgroundWorker's task quite easily.
Hi,
Thanks for the push in the right direction...im going to go read up on BackgroundWorkers.
Cheers,
Justin
Arc
Jul 13th, 2007, 03:37 PM
You could also just put a global bool flag in your loops and subroutine so when it is set to true the loops/subroutine exit.
effekt26
Jul 13th, 2007, 06:07 PM
You could also just put a global bool flag in your loops and subroutine so when it is set to true the loops/subroutine exit.
I had actually thought of that...but i wanted the loop to stop pretty much straight away, not go through till the if statement...plus, the more loops i have, and more functions called by these loops, the more if statements i need.
Cheers,
Justin
jmcilhinney
Jul 13th, 2007, 10:04 PM
You can't just use a flag as suggested because it will not be set until the loop has completed unless you call Application.DoEvents regularly in the loop.
Now, using a BackgroundWorker does not solve the "problem" of having to check whether a cancellation request has been made. You still have to use an If statement to test the e.CancellationPending property in the DoWork event handler. This is quite reasonable because in the majority of cases you wouldn't want execution of your task to just halt immediately. If that happened then you'd have no idea what state your data was in so it may actually be dangerous to halt at certain spots.
If you know for a fact that it is safe to just halt immediately regardless of state then you should create a Thread explicitly and call its Abort method.
Lord Orwell
Jul 13th, 2007, 11:05 PM
yes. And if you have all these loops in a subroutine, you could exit the subroutine with an "exit sub" command or "exit function" depending. Of course, i don't know how good of an idea that would be while accessing a database. If you were writing data this could cause major problems.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.