Stopping a task

I was looking for a way to stop a task by clicking on a button. My program performs some very complex calculation, and I want to add the feature of stopping the number crunching in the middle by clicking on a button or a custom ActiveX control. Any suggestions?
skamal021899Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AnswerTheManCommented:
if you want to stop ececution of a function, when some button is clicked, you should add a Module Level Boolean.
when the button is clicked - you set this boolean to TRUE.
now, in the function (to be stopped), add here and there a :
 if SomeBoolean=True then
     SomeBoolean=False    
     exit sub\function
 end if

if the function contains loop(s), you should insert the *DoEvents* statment
into the loops in order to enable buttun click event.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erick37Commented:
Here's a little example:

Option Explicit
Private bStop As Boolean

Private Sub Command1_Click()
    'Flag that we want to stop the operation
    bStop = True
End Sub

Private Sub Form_Load()
    Show
    bStop = False
    'Start the long operation
    Call LongOperation
End Sub

Private Sub LongOperation()
    Dim i, j As Double
    For i = 1 To 30000
        j = i - (4 * Atn(1))
        Text1.Text = Format(j, "#0.0000")
        DoEvents 'Yield to other events
        If bStop Then Exit For 'Exit if True
    Next
    bStop = False
End Sub
0
skamal021899Author Commented:
I really appreciate your example, erick37. It very helpful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.