Results 1 to 7 of 7

Thread: creating a cancel button to exit my loops..

  1. #1

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: creating a cancel button to exit my loops..

    Quote 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

  4. #4
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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.


  5. #5

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: creating a cancel button to exit my loops..

    Quote 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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width