Cancelling VB while it's running...

Discussion in 'AutoCAD' started by pkirill, Sep 10, 2003.

  1. pkirill

    pkirill Guest

    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!
     
    pkirill, Sep 10, 2003
    #1
  2. 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
     
    Mark Johnston, Sep 10, 2003
    #2
  3. 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
     
    James Stergar, Sep 10, 2003
    #3
  4. pkirill

    pkirill Guest

    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...
     
    pkirill, Sep 10, 2003
    #4
  5. 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
     
    Gary McMaster, Sep 10, 2003
    #5
  6. pkirill

    Chris Tryon Guest

    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
     
    Chris Tryon, Sep 11, 2003
    #6
  7. pkirill

    pkirill Guest

    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..................
    <Lots more stuff here to process>
    <End of things to process>

    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
     
    pkirill, Sep 12, 2003
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.