Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ending a process

Posted on 2000-04-17
4
Medium Priority
?
148 Views
Last Modified: 2010-05-02
I have a function in a separate class that updates the treeview control nodes on a form. The class has a reference to the tree control, but not the form.  The problem is that if try to close the form while the function is updating, the form closes, but then reopens because the function is still  referencing the treeview. Anyone know how I can keep this from happening?  I've tried setting the object= nothing when the form closes, as well as the reference to the treeview, but it didn't work.  
0
Comment
Question by:gferrell
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2723697
If you reference a form, or a control on the form, after the form has been unloaded (or closed), the form will reload.

You need to ensure that you abort the update when you want to close the form.  Try something like this:

In your Class, include a Private variable, and a public function like this:

Private clsAbortFlag as Boolean

Public Function clsAbort() as Integer

If CanNotAbort() Then   ' Test - can I stop now?  (Optional)
   clsAbort = False
Else
   clsAbortFlag = True
   Do While clsAbortFlag
      DoEvents ' Or however you prefer to wait for processing to complete
   Loop
   clsAbort = True
End If
End Function

In each of the procedures in your class that might be affected, insert a test:

  If clsAbortFlag Then Exit Sub

In your Form_QueryUnload, add something like this:

If Not clsAbort Then
   MsgBox "Can not close at this time."
   Cancel = True
   Exit Sub
End If

Then continue as normal.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2723702
There are multiple typos (Sorry), but you should get the idea.
0
 

Accepted Solution

by:
Scrappy earned 800 total points
ID: 2723934
-------------------------------------
FORM1
-------------------------------------
Option Explicit

Private WithEvents clsTreeViewAlteringClass As class1
Private bWaitingToClose As Boolean

Private Sub clsTreeViewAlteringClass_LongStoppedProcessing()
    If bWaitingToClose Then Unload Me
End Sub

Private Sub Command1_Click()
    Call clsTreeViewAlteringClass.addnodes
End Sub

Private Sub Form_Load()
    Set clsTreeViewAlteringClass = New class1
    bWaitingToClose = False
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If clsTreeViewAlteringClass.bActive = True Then
        bWaitingToClose = True
        clsTreeViewAlteringClass.bStopProcess = True
        Cancel = True
    End If
End Sub

-------------------------------------
CLASS1
-------------------------------------
Option Explicit

Public m_bStopLongProcess As Boolean
Public m_LongProcessActive As Boolean
Public Event LongStoppedProcessing()

Public Function addnodes() As Boolean

Dim i As Integer
Dim ns As Nodes

    m_LongProcessActive = True
   
    Set ns = Form1.TreeView1.Nodes
   
    m_bStopLongProcess = False
   
    ns.Add , "r", "r", "this is text"
   
    Do While i < 5000 And Not m_bStopLongProcess
        ns.Add "r", tvwChild, "c" & CStr(i), "child 1" & CStr(i)
        DoEvents
        i = i + 1
    Loop
   
    Set ns = Nothing
   
    m_LongProcessActive = False
   
    RaiseEvent LongStoppedProcessing

End Function


0
 

Author Comment

by:gferrell
ID: 2724029
Good thinking using an event!  
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question