Cancelling VB while it's running...

Cancelling VB while it's running...

Anonymous
Not applicable
510 Views
6 Replies
Message 1 of 7

Cancelling VB while it's running...

Anonymous
Not applicable
We've got this batch processor that was created in VB6. It's pretty handy
except for the fact that once it's going, you can't stop it. I've looked up
using the escape key, but I can't seem to get it to trap out. What I'm
wondering is if someone can either give me a newbie explanation of how to
use the escape key or how to use the existing "exit" button to stop the
routine from running. (Currently the exit button is just used to close the
dialog window.)

If I haven't provided enough info, please let me know.

As always any help is greatly appreciated!

Thanks!
0 Likes
511 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
One way would be to add a "Cancel" button that is enabled when the process
starts.
Also add a variable in your General Declarations as a kind of switch.
Something like bCancel as Boolean should do.

Before you start your batch set bCancel to False then
Before beginning each item (loop) of your batch check to see if bCancel is
True. If it is then Exit the batch.

The code in the Cancel button is simply: bCancel = True

Example:
Private Sub ProcessItems()
Dim I As Integer

bCancel = False
For I = 0 to 23000
If bCancel = True Then Exit For
' Your fancy process code here
Next
End Sub

"pkirill" wrote in message
news:[email protected]...
> We've got this batch processor that was created in VB6. It's pretty handy
> except for the fact that once it's going, you can't stop it. I've looked
up
> using the escape key, but I can't seem to get it to trap out. What I'm
> wondering is if someone can either give me a newbie explanation of how to
> use the escape key or how to use the existing "exit" button to stop the
> routine from running. (Currently the exit button is just used to close
the
> dialog window.)
>
> If I haven't provided enough info, please let me know.
>
> As always any help is greatly appreciated!
>
> Thanks!
>
>
>
>
0 Likes
Message 3 of 7

Anonymous
Not applicable
You would need a "DoEvents" method inside the loop to check for an Escape
keystoke or a button being pressed. For example:

Dim canceled As Boolean

Private Sub doLoop()

' start infinite loop
canceled = False
Do While False = True
DoEvents
If canceled Then
Exit Do
End If
Loop

End Sub

Private Sub cmdCancel_Click()

canceled = True

End Sub

Hope this helps,

James

"pkirill" wrote in message
news:[email protected]...
> We've got this batch processor that was created in VB6. It's pretty handy
> except for the fact that once it's going, you can't stop it. I've looked
up
> using the escape key, but I can't seem to get it to trap out. What I'm
> wondering is if someone can either give me a newbie explanation of how to
> use the escape key or how to use the existing "exit" button to stop the
> routine from running. (Currently the exit button is just used to close
the
> dialog window.)
>
> If I haven't provided enough info, please let me know.
>
> As always any help is greatly appreciated!
>
> Thanks!
>
>
>
>
0 Likes
Message 4 of 7

Anonymous
Not applicable
Okay, I'm still a bit confused... sorry...

I tried the code from Mark, but when I switch to the Batch Dialog and click
"Exit" I get a message saying that the command cannot be completed because
the other application is busy - try "switch to"...
And then the cancel is never picked up...


"James Stergar" wrote in message
news:[email protected]...
> You would need a "DoEvents" method inside the loop to check for an Escape
> keystoke or a button being pressed. For example:
>
> Dim canceled As Boolean
>
> Private Sub doLoop()
>
> ' start infinite loop
> canceled = False
> Do While False = True
> DoEvents
> If canceled Then
> Exit Do
> End If
> Loop
>
> End Sub
>
> Private Sub cmdCancel_Click()
>
> canceled = True
>
> End Sub
>
> Hope this helps,
>
> James
>
> "pkirill" wrote in message
> news:[email protected]...
> > We've got this batch processor that was created in VB6. It's pretty
handy
> > except for the fact that once it's going, you can't stop it. I've
looked
> up
> > using the escape key, but I can't seem to get it to trap out. What I'm
> > wondering is if someone can either give me a newbie explanation of how
to
> > use the escape key or how to use the existing "exit" button to stop the
> > routine from running. (Currently the exit button is just used to close
> the
> > dialog window.)
> >
> > If I haven't provided enough info, please let me know.
> >
> > As always any help is greatly appreciated!
> >
> > Thanks!
> >
> >
> >
> >
>
>
0 Likes
Message 5 of 7

Anonymous
Not applicable
I have a batch processor that uses the "AcadState" object to overcome that
(busy) problem.

Something like so:

Public Sub CheckAcadBusy()

'Continuously check the acad state object until AutoCAD is idle then exit
this sub
'Allows .lsp or .dvb to finish all operations before this program attempts
to continue

Dim State As AcadState

AcadBusy:

Set State = GetAcadState

If State.IsQuiescent Then

If bAbort = True Then ConfirmAbort 'Abort button was clicked,
obtain confirmation
Exit Sub 'AutoCAD is quiescent

Else

GoTo AcadBusy 'AutoCAD is not quiescent

End If

End Sub


The "bAbort" flag is set by code in the "Cancel" or "Abort" button click on
the main form.
I also use "AppActivate" to show the batch processors main form after each
drawing is processed to give the user the opportunity to interact.

You may also be able to make use of the App.OleServerBusyTimeout or some of
it's related functions. A search in help should find them. I think they all
start with "Ole".

Hope it helps,

Gary
0 Likes
Message 6 of 7

Anonymous
Not applicable
Like James said your whole problem stems from the doevents. You need this
statement in your loops ie

for i =0 to n
...
doevents
next i

the doevents allows the cpu to see what else is going on (run other
processes). if you add doevents in your loop, then you cancel button would
be useful.

HTH

//chris

"pkirill" wrote in message
news:[email protected]...
> We've got this batch processor that was created in VB6. It's pretty handy
> except for the fact that once it's going, you can't stop it. I've looked
up
> using the escape key, but I can't seem to get it to trap out. What I'm
> wondering is if someone can either give me a newbie explanation of how to
> use the escape key or how to use the existing "exit" button to stop the
> routine from running. (Currently the exit button is just used to close
the
> dialog window.)
>
> If I haven't provided enough info, please let me know.
>
> As always any help is greatly appreciated!
>
> Thanks!
>
>
>
>
0 Likes
Message 7 of 7

Anonymous
Not applicable
Putting it together - would it look something like this?

Private Sub doLoop()

Dim canceled As Boolean

For I = 0 To lstDrawings.ListCount - 1
strDwgNumber(I) = LTrim(strDrawingNumberList(I))
strDwgs(I) = strFilePath & strDrawingNameList(I) & ".dwg"
strStampToShow(I) = strStampList(I)
canceled = False

Do While False = True

If lstDrawings.Selected(I) = True Then
If FileCheck.FileExists canceled = False..................



DoEvents
If canceled Then
Exit Do
End If
Loop

Next I

End Sub

I'm still pretty confused - but then it's Friday and I'm a little burnt.
For what it's worth I'm including the basic For/Next loop below. I really do
appreciate the help!

Private Sub cmdPlot_Click()

If chkOpenOnly.Value = Checked Then
intNumber = InStrLast(striFileName, "\")
strFilePath = Mid(striFileName, 1, intNumber)
ReDim strDwgs(0 To lstDrawings.ListCount - 1)
ReDim strDwgNumber(0 To lstDrawings.ListCount - 1)
For I = 0 To lstDrawings.ListCount - 1
strDwgs(I) = strFilePath & strDrawingNameList(I) & ".dwg"
If lstDrawings.Selected(I) = True Then
If FileCheck.FileExists(strDwgs(I)) = True Then
If InStrLast(strDrawingNameList(I), "_") > 1 And strCommission =
"00012" Or strCommission = "01014" Or strCommission = "01014-01" Then
FileCheck.DeleteFile (strDwgs(I))
Set dwg = acad.Documents.Add(strDwgs(I))
Else
Set dwg = acad.Documents.Open(strDwgs(I), False)
End If
ElseIf lstDrawings.Selected(I) = False And strCommission = "00012" Or
strCommission = "01014" Or strCommission = "01014-01" Then
Set dwg = acad.Documents.Add(strDwgs(I))
End If

If InStrLast(strDrawingNameList(I), "_") > 1 And strCommission =
"00012" Or strCommission = "01014" Or strCommission = "01014-01" Then

If strCommission = "00012" Then
dwg.SendCommand "(load ""h:/2000/00012/arch/support/newdwg"")" &
vbCr
dwg.SendCommand ("(NewDullesDwg " & """" & strDrawingNameList(I)
& """)" & vbCr)
ElseIf strCommission = "01014" Then
dwg.SendCommand ("(load
""h:/2001/01014/arch/support/NewTier2"")" & vbCr)
dwg.SendCommand ("(NewDullesDwg " & """" & strDrawingNameList(I)
& """)" & vbCr)
ElseIf strCommission = "01014-01" Then
dwg.SendCommand ("(load
""h:/2001/01014-01/arch/support/NewTier2"")" & vbCr)
dwg.SendCommand ("(NewDullesDwg " & """" & strDrawingNameList(I)
& """)" & vbCr)
End If
End If
If chkDwup.Value = Checked Then
dwg.SendCommand "dwup" & vbCr
End If
If chkUpdateSpanner = Checked Then
dwg.SendCommand ("sheetall" & vbCr)
End If
End If

Next I

If chkClose.Value = Checked Then
Unload (frmArchiveDesc)
End
End If
End If

End Sub


"James Stergar" wrote in message
news:[email protected]...
> You would need a "DoEvents" method inside the loop to check for an Escape
> keystoke or a button being pressed. For example:
>
> Dim canceled As Boolean
>
> Private Sub doLoop()
>
> ' start infinite loop
> canceled = False
> Do While False = True
> DoEvents
> If canceled Then
> Exit Do
> End If
> Loop
>
> End Sub
>
> Private Sub cmdCancel_Click()
>
> canceled = True
>
> End Sub
>
> Hope this helps,
>
> James
>
> "pkirill" wrote in message
> news:[email protected]...
> > We've got this batch processor that was created in VB6. It's pretty
handy
> > except for the fact that once it's going, you can't stop it. I've
looked
> up
> > using the escape key, but I can't seem to get it to trap out. What I'm
> > wondering is if someone can either give me a newbie explanation of how
to
> > use the escape key or how to use the existing "exit" button to stop the
> > routine from running. (Currently the exit button is just used to close
> the
> > dialog window.)
> >
> > If I haven't provided enough info, please let me know.
> >
> > As always any help is greatly appreciated!
> >
> > Thanks!
> >
> >
> >
> >
>
>
0 Likes