Solved

Ending a process

Posted on 2000-04-17
4
136 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:bhess1
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:bhess1
ID: 2723702
There are multiple typos (Sorry), but you should get the idea.
0
 

Accepted Solution

by:
Scrappy earned 200 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now