gcarpi
08-05-2004, 09:36 AM
Hello
In my program, 2 subs are executing at the same time: the actions and a progress bar on a different form. I added a cancel button below my progress bar and I want to exit both subs when i click on it. How can I do?
Thanks
rick_deacha
08-05-2004, 09:40 AM
Althought not the best use End
gcarpi
08-05-2004, 09:42 AM
Althought not the best use End
sure works but I want the program to stay open....
VBJoe
08-05-2004, 09:42 AM
You could use a global Boolean variable that you set to True if the user clicks the cancel button. You'll need to check the state of the Boolean variable in both subs (especially if you're looping), and Exit Sub if the variable has been set to True.
gcarpi
08-05-2004, 09:46 AM
You could use a global Boolean variable that you set to True if the user clicks the cancel button. You'll need to check the state of the Boolean variable in both subs (especially if you're looping), and Exit Sub if the variable has been set to True.
Yeah but all my operation are very long so the state of the boolean variable has to be check before or after...not while...and that's what I want....
VBJoe
08-05-2004, 09:49 AM
What do you mean by "my operations are very long"? Are you doing some mathematical calculations? I don't see how you're going to interrupt processing except for with a global Boolean.
gcarpi
08-05-2004, 09:50 AM
What do you mean by "my operations are very long"? Are you doing some mathematical calculations? I don't see how you're going to interrupt processing except for with a global Boolean.
That's some mathematical calculation...long simulations. ...
VBJoe
08-05-2004, 09:52 AM
If you're using loops in the routines, you should be able to check the global Boolean on every pass. If you're not using loops, then I don't think there's any way to force the mathematical processing to stop without using the End statement (which I would NEVER recommend).
gcarpi
08-05-2004, 09:54 AM
If you're using loops in the routines, you should be able to check the global Boolean on every pass. If you're not using loops, then I don't think there's any way to force the mathematical processing to stop without using the End statement (which I would NEVER recommend).
Ok. I'll try that
Thanks
Mathimagics
08-05-2004, 10:02 AM
VB6 does not generate multi-threading executables, so 2 subs can't actually execute at once ... ;)
You might think timers are asynchronous, but they are not - if one of your subs is in a loop, and you have a timer running as well, then the timer will only get a lookin if the looping sub makes a DoEvents call ...
So, whatever mechanism is being used to update the progress, it's just a matter of telling it when to stop, e.g. when progress = max just unload the progress display form ....
noi_max
08-05-2004, 10:41 AM
VB6 does not generate multi-threading executables, so 2 subs can't actually execute at once ...
Ok, that's what I was thinking... (I was trying to figure out how to run two subs at once and thought my brain was going to melt....)
So I figure the last suggestion made by MathImagics there makes the most sense.
As always you can step through your code using F8 and see where an 'Exit Sub' needs to occur.
ChuckH
08-05-2004, 07:10 PM
Another possibility would be to create an error handler up above where both of your subs are called and generate a user-defined error (using Err.Raise) when the Cancel button is clicked. You'd just have to watch out if the subs have their own error handlers, they would need to be modified to pass the user-defined error up the calling stack by raising it again.
deell
08-05-2004, 07:18 PM
add DoEvents into your loop
Dim flagStop As Boolean
Private Sub Process()
flagStop = False
Do while ...
'Your processing code
DoEvents
If flagStop = True Then Exit Do
Loop
End Sub
Private Sub cmdStop_Click()
flagStop = True
End Sub