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

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?
0
kenshaw
Asked:
kenshaw
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
[ 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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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