Solved

Ending a process

Posted on 2000-04-17
4
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

738 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