Click to See Complete Forum and Search --> : Should be easy one.....


AndyK
January 28th, 2000, 01:54 AM
If you are lazy to read detailed and confusing explanation then goto skip:
Ok I have Command2 and when I click it, my program starts to do whatever it's in Command2...long code, well I have timer,loop,for, etc. in Command2 now.....by pressing "STOP" (command3)I want kind of skip everything that wasn't yet "executed" in Command2.

P.S. I can't use timer and make it like timer1.enabled = false where timer has all the coding from command2 because to process everything in command2 my program needs around 3 mins (depends on user's input) and timer's interval doesn't support 3 mins so it will start all over in the middle of process of command2
skip:
Hope that wasn't confusing, but to sum everything up all I need is to have option to cancel everything my program is doing and basically make it as if it was just loaded...."fresh and clean" :)

Thank You

Dr_Michael
January 28th, 2000, 02:01 AM
The DoEvents

command let other processes to take place. So, if you place this command to the beginning of your command1_click code, it will give you the chance at any point to stop executing the code by pressing the second button. Of course I suppose that by pressing the second one you may have written code for emptying the memory (such as release recordsets, variables etc). Hope I helped you.

Michael Vlastos
Automation Engineer
Company SouthGate Hellas SA
Development Department
Athens, Greece

AndyK
January 28th, 2000, 02:48 AM
So how can I stop DoEvents ..... what is the code to stop it?

Thank you

Chris Eastwood
January 28th, 2000, 02:59 AM
I think this is what you should be doing (or similar). Take a form with two buttons (cmdStart and cmdStop) and try the following code :


option Explicit
'
private mbQuitLongStuff as Boolean
'
private Sub cmdStart_Click()
'
' Example of doing something very long....
'
Dim l as Long
'
mbQuitLongStuff = false
'
for l = 1 to 10000000
If l Mod 1000 then
DoEvents
End If
If mbQuitLongStuff then
Exit for ' and cleanup afterwards
End If
next
'
If mbQuitLongStuff then
MsgBox "We Quit !"
End If
'
End Sub
'
private Sub cmdStop_Click()
mbQuitLongStuff = true
End Sub




You can change the interval that 'doevents' occurs at in the 'If l Mod...' line. Making 'doevents' happen at shorter intervals will make your loop take longer (but your app will be more responsive). Becareful using this method though, DoEvents allows your app to process messages for other controls (and close the form etc), so make sure that you've disabled all relevant controls before you start the loop and reenable them afterwards.



Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Dr_Michael
January 28th, 2000, 03:04 AM
Maybe a little missunderstanding...
DoEvents let you press other buttons and generally to take place asychronous processes. That means that you use DoEvents if you want to be able to stop a long loop, or a loop without end. So, I think I didn't understand you, or you didn't understand me. Sorry if it was my fault.
If you need more explanations, ask me.

Michael Vlastos
Automation Engineer
Company SouthGate Hellas SA
Development Department
Athens, Greece