how do you cause a different function to abort processing?

i have a main loop doing a bunch of processing work

i have a button - which is a cancel button.  If the user presses that button - i want control to leave the main loop where it is - i.e. i want all control to transfer out of that loop - for the loop to exit.

how would i do this?
LVL 4
kenshawAsked:
Who is Participating?
 
sakuya_suCommented:
you can have a global variable like a integer that can be either 1 or 0, your main loop will check to see if that variable is 0 or not, if it is then it will keep going.

so your button just have to set the variable to 1.

but I suspect... that if your main loop is going and since the VB application is normaly single thread.. your button will simply not respond at all.

to do it properly you need to use threads, so the cancel button(and your form) will still respond while the main function is running and your Button can just call the destory thread function
0
 
iHadiCommented:
Hi
I go with sakuya_su's solution.
You can put DoEvents in your loop instead and this will do the  job instead of using complicated to code and manage threads:

Do
' Some Code
DoEvents
' Some Code
Loop
0
 
[ fanpages ]IT Services ConsultantCommented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
[ fanpages ]IT Services ConsultantCommented:
Define a global (public) variable, say, blnCancel as a Boolean data type, in a public code module (where your main loop resides).

Public blnCancel As Boolean

Initially set this to False before your main loop starts.

Then inside the loop, place one (or more) calls to the DoEvents statement, and in the _Click() event of your button set the value of the blnCancel variable to True.


' Main loop example 1

While Not (blnCancel)

    DoEvents

    If Not (blnCancel) Then
' Do main processing
    End If

Wend

' Main loop example 2

For intCounter = 1 to 65536

    DoEvents

    If (blnCancel) Then
       Exit For
    End If

' Do some processing

    DoEvents

    If (blnCancel) Then
       Exit For
    End If

' Do some more processing

    DoEvents

' etc...

Next intCount


' Click event for "Cancel" command button in local form code module

Sub btnCancel_Click()

  blnCancel = (MsgBox("Do you wish to cancel?", vbQuestion Or vbYesNo) = vbYes)

' or, simply...
'  blnCancel = True

End Sub



BFN,

fp.
0
 
[ fanpages ]IT Services ConsultantCommented:
You're very welcome.

Thanks for acknowledging my comments.
0
 
kenshawAuthor Commented:
are you talking to me?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.