Solved

Ending a process

Posted on 2000-04-17
4
145 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: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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month8 days, left to enroll

617 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