|
-
Jul 13th, 2007, 12:26 AM
#1
Thread Starter
Addicted Member
creating a cancel button to exit my loops..
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
-
Jul 13th, 2007, 12:30 AM
#2
Re: creating a cancel button to exit my loops..
Use a BackgroundWorker to perform the loop. You can cancel a BackgroundWorker's task quite easily.
-
Jul 13th, 2007, 01:07 AM
#3
Thread Starter
Addicted Member
Re: creating a cancel button to exit my loops..
 Originally Posted by jmcilhinney
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
-
Jul 13th, 2007, 03:37 PM
#4
PowerPoster
Re: creating a cancel button to exit my loops..
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.
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jul 13th, 2007, 06:07 PM
#5
Thread Starter
Addicted Member
Re: creating a cancel button to exit my loops..
 Originally Posted by Arc
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
-
Jul 13th, 2007, 10:04 PM
#6
Re: creating a cancel button to exit my loops..
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.
-
Jul 13th, 2007, 11:05 PM
#7
Re: creating a cancel button to exit my loops..
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|