|
-
Feb 13th, 2010, 11:17 AM
#1
Thread Starter
Member
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
-
Feb 13th, 2010, 11:21 AM
#2
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.
-
Feb 13th, 2010, 11:31 AM
#3
Thread Starter
Member
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?
-
Feb 13th, 2010, 11:35 AM
#4
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.
-
Feb 13th, 2010, 12:21 PM
#5
Frenzied Member
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.
-
Feb 13th, 2010, 12:24 PM
#6
Thread Starter
Member
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?
-
Feb 13th, 2010, 12:55 PM
#7
Re: Cancel / Discard Code Being Executed
 Originally Posted by Cooi
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.
-
Mar 11th, 2010, 09:59 PM
#8
New Member
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.
-
Mar 11th, 2010, 10:07 PM
#9
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.
-
Mar 12th, 2010, 07:56 AM
#10
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|