Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancelling a long running process in VB6.0 without DoEvents?

Tags:

vb6

Is it possible to cancel out of a long running process in VB6.0 without using DoEvents?

For example:

for i = 1 to someVeryHighNumber
    ' Do some work here '
    ...

    if cancel then
        exit for
    end if
next

Sub btnCancel_Click()
    cancel = true
End Sub

I assume I need a "DoEvents" before the "if cancel then..." is there a better way? It's been awhile...

like image 241
Stuart Helwig Avatar asked Sep 30 '08 23:09

Stuart Helwig


3 Answers

Nope, you got it right, you definitely want DoEvents in your loop.

If you put the DoEvents in your main loop and find that slows down processing too much, try calling the Windows API function GetQueueStatus (which is much faster than DoEvents) to quickly determine if it's even necessary to call DoEvents. GetQueueStatus tells you if there are any events to process.

' at the top:
Declare Function GetQueueStatus Lib "user32" (ByVal qsFlags As Long) As Long

' then call this instead of DoEvents:
Sub DoEventsIfNecessary()
    If GetQueueStatus(255) <> 0 Then DoEvents
End Sub
like image 198
Joel Spolsky Avatar answered Nov 18 '22 18:11

Joel Spolsky


No, you have to use DoEvents otherwise all UI, keyboard and Timer events will stay waiting in the queue.

The only thing you can do is calling DoEvents once for every 1000 iterations or such.

like image 21
GSerg Avatar answered Nov 18 '22 16:11

GSerg


Is the "for" loop running in the GUI thread? If so, yes, you'll need a DoEvents. You may want to use a separate Thread, in which case a DoEvents would not be required. You can do this in VB6 (not simple).

like image 7
TheSoftwareJedi Avatar answered Nov 18 '22 18:11

TheSoftwareJedi