• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

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?
0
skamal021899
Asked:
skamal021899
1 Solution
 
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
 
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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now