Results 1 to 10 of 10

Thread: Cancel / Discard Code Being Executed

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Exclamation Cancel / Discard Code Being Executed

    Hi all.

    Is it possible to stop a Sub from executing its code when you, for example, click a button?

    Example:

    Code:
    Private Sub ExecutedCode()
      'doing things here
    End Sub
    
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
      'how to stop ExecutedCode() from being executed?
    End Sub

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Cancel / Discard Code Being Executed

    It depends on what is happening in ExecutedCode. Its easy if its a loop - just set a module level flag which you can set in the button's click event and test on each iteration of the loop, ie

    Code:
    Public Class Form1
    
        Private _Cancel As Boolean = False
    
        Private Sub ExecuteCode()
    
            For nX As Integer = 0 To 500000
    
                If _Cancel = False Then
                    'do work
                Else
                    'user has cancelled so bail out
                    Exit For
                End If
            Next
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            _Cancel = True
    
        End Sub
    End Class
    If ExecuteCode is a sequence of different actions you can just test for the flag at the start of each action.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Re: Cancel / Discard Code Being Executed

    That is a helpful hint, but are you confirming there is no possibility to discard the code being executed immediatly?

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Cancel / Discard Code Being Executed

    If you put your ExecuteCode into a separate thread you can probably abort that thread immediately.

    It depends on how complex your application is as to whether its worth adding multi-threading.

  5. #5
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Cancel / Discard Code Being Executed

    What Keystone gave as an example will cancel the executing code immediately. Are you wanting a way to roll back what has been executed up to the point of a user clicking on the Cancel button?

    If so and you are not using a Database, you'll want to create an intermediate object that tracks the changes being made along the way. Then, when cancel is pressed you'll use the information captured in the intermediate tracking object to undo the changes made up to the point in time that the Cancel command was clicked.

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Re: Cancel / Discard Code Being Executed

    I know that there are alot of other solutions I can do to stop the code being ran by booleans and stuff like that, but is there absolutely no way to cancel the execution completely?

  7. #7
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Cancel / Discard Code Being Executed

    Quote Originally Posted by Cooi View Post
    I know that there are alot of other solutions I can do to stop the code being ran by booleans and stuff like that, but is there absolutely no way to cancel the execution completely?
    I think you'll have to explain what you mean by cancel the execution completely.

    If you mean you want effectively to undo any part of the process that has already begun then as fourblades says you'll need some method for logging what you've done and then effectively reverse each stage at a time.

    If thats not what you mean then I have no idea what you're after. Can you elaborate.

  8. #8
    New Member
    Join Date
    Mar 2010
    Location
    Virginia
    Posts
    2

    Re: Cancel / Discard Code Being Executed

    I am having similar difficulty canceling an executing program by clicking a button. The code by Paul is the type of code I am searching for, but my problem is that clicking the button (Button1 in this case) does not have any effect on the execution of the program. It operates as though clicking Button1 does not exist. I've tried multithreading, but my 2000 line code has dozens of complex arrays to transfer and display (graphics) during each iteration of the working thread, and that is too much to place in a separate class (to transfer to the "BackgroundWorker1_ProgressChanged" event handler). There must be an easy way to do this ... help please.

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

    Re: Cancel / Discard Code Being Executed

    If you are currently executing a method and you click a Button, the Click event handler of the Button is queued and won't be executed until the current method completes. The only way around that in a single thread is to call Application.DoEvents at intervals, which will pause the current method while any pending events are handled. That still won't allow you to abort the running method though. You'd still have to set a flag in the event handler and then test its value after DoEvents.

    As for multi-threading, one option is to explicitly create a Thread and then call its Abort method. This is generally not a good idea though. You still have to handle the ThreadAbortException it throws and then clean up, with no idea of where exactly the code stopped executing. If the code is complex then halting execution at some arbitrary point is almost certainly a very bad idea.

    With a BackgroundWorker you can use the built-in cancellation mechanism, allowing you to choose the points at which the background operation can be safely halted.

    The fact that you want an easy solution doesn't mean that there is, or should be, one. There are greater concerns that the developer's convenience in this matter.
    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

  10. #10
    New Member
    Join Date
    Mar 2010
    Location
    Virginia
    Posts
    2

    Re: Cancel / Discard Code Being Executed

    For jmcilhinney: Thank you! That is exactly what I was searching for.

Tags for this Thread

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