Results 1 to 14 of 14

Thread: [RESOLVED] Previous Instances Remaining

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    32

    Resolved [RESOLVED] Previous Instances Remaining

    I am currently using VB6 with SP6 on XP Home to create a program that generates reports from text files. I have everything working fine within the program at this time, but there remains one problem. If you click the EXIT button then the program ends as it should and releases the memory and processes just as it should. I have viewed this activity in the Task Manager. If you exit by clicking the X in the upper right corner then the applications appears to have closed since the main window of the application goes away but if you view in Task Manager the prior instance of the application is still running. I have added the code
    Code:
    If App.PrevInstance Then
         End
    End If
    But if there is a previous instance running but not accessible by the user then another instance will not run either and then there is no way to close the prior instance or start a new instance without opening the Task Manager to "End Task" on the instance that was held over.

    How do you get the instance to release properly when the X is clicked? I know I can remove that from the form in design but would rather keep it there for ease of transistion for users since it is similar to all other Windows applications.

    Thanks in advance for your time and effort in this.
    God Bless You All.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Previous Instances Remaining

    Quote Originally Posted by DaWolf
    How do you get the instance to release properly when the X is clicked?
    There are number of thing to look upon closing your app the following "list" is just an idea:

    - close all global connections, recordsets, etc (if any)
    - destroy all global objects
    - close all documents (if automation is in place)
    - loop through forms collection and close each form individually
    - for each form (form_unload or queryunload events can be used)
    ... - close any open local connection, etc...
    ... - destroy all form level objects

    ... and so on and so forth ...

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Previous Instances Remaining

    To expand a little on what RhinoBull said.. the problem is that not everything is being closed/unloaded, possibly because of using "End" (which is basically a forced crash, with some cleanup, but not enough).

    What you should do instead is unload everything properly, which should usually be done a Form_Unload event.


    In there you should close and release all object variables as appropriate (anything you "Set" or "Dim .. as New ...", which includes recordsets etc), disable all timers (as timers will reload the form!), and if appropriate stop any long-running loops (similar to timers).

    If you have multiple forms, you can use code like this in your 'main' form to unload the others:
    Code:
        Dim frmTemp As Form
        For Each frmTemp In Forms
            If Not frmTemp is Me Then
              Unload frmTemp
              Set frmTemp = Nothing
            End if
        Next frmTemp
    (note that every form should have code in its _Unload event to close its own object variables, timers, etc).

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Previous Instances Remaining

    You can try something like this,

    Code:
    Option Explicit
    Dim terminate_app As Boolean
    
    Private Sub Form_Load()
        If App.PrevInstance Then
            terminate_app = True
            MsgBox "A copy of My Program is already loaded!", vbInformation
            Unload Me
            Exit Sub
        End If
    
    ' YOUR CODE HERE
    
    End Sub
    
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    	If terminate_app = True Then Exit Sub ' only run on normal exit
    
    ' Stop timers, close open files, save user settings, etc,etc,
    
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    
        Dim i As Integer
        ' unload forms 
        For i = Forms.Count - 1 To 0 Step -1
            Unload Forms(i)
        Next i
        
        'Set Form1 = Nothing
    End Sub

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Previous Instances Remaining

    generally don't use End, use unload me to unload your form, as well as all the things rhino said, make sure no code runs after you unload a form that in any way may reference any property or control of the form, put exit sub, immediately after unload me
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    32

    Re: Previous Instances Remaining

    I have resolved this problem by just using the Form_QueryUnload event which is triggered when the user attempts to exit the application by either a method coded by the programmer or by using the X in the upper right corner. I just placed my calls to the subroutine(s) for cleaning up variables, files, and forms in the QueryUnload event. I then called the QueryUnload event upon the Exit Button be clicked. I watched in Task Manager and the program was released and ended from the lsit of tasks being run when exited in either fashion.
    Thanks for everyones help.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Previous Instances Remaining

    There is no need to call _QueryUnload or _Unload, as they will run automatically when you use "Unload Me", or the form closes some other way (like clicking the "X")

    There is no reason to use _QueryUnload (which is the point where you can cancel the unload), rather than _Unload itself (which happens when unloading is actually going to happen).

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Previous Instances Remaining

    Quote Originally Posted by si_the_geek
    There is no reason to use _QueryUnload ...
    There is very good reason to use it - mainly because it allows to trap the unloadmode - in other words what caused application to exit.

  9. #9
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: [RESOLVED] Previous Instances Remaining

    Quote Originally Posted by RhinoBull
    There is very good reason to use it - mainly because it allows to trap the unloadmode - in other words what caused application to exit.
    All my forms have tons of checking isDirty code in the query_Unload, i would be lost with-out it. It's the best place to mange the unload process
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Previous Instances Remaining

    That is fair enough for choosing whether or not to show a message to the user (eg: only show "are you sure you want to exit?" if Windows isn't closing the app), but what part of the actual unload would you do differently depending on unloadmode?

  11. #11
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: [RESOLVED] Previous Instances Remaining

    Quote Originally Posted by si_the_geek
    That is fair enough for choosing whether or not to show a message to the user (eg: only show "are you sure you want to exit?" if Windows isn't closing the app), but what part of the actual unload would you do differently depending on unloadmode?
    I have roof estimating software that mostly revolves around the main form. One unload mode i can think of off the top of my head is one i added recently.
    If a user has a saved bid open from the main form this bid also fills a secondary
    form that contains 5 flexigrid with saved information. If the user tries to unload the second form by clicking the X i only make the form invisible and show the main form. If i let them close the form the data will be gone so i do this:
    If Not mGridsAreDirty Then
    If gSavedBidIsOpen And UnloadMode = vbFormControlMenu Then
    Cancel = 1
    Me.Visible = False
    MainForm.Show
    GoTo bail
    End If
    End If
    If unloaded from main form i allow if it's not dirty
    Last edited by isnoend07; Feb 2nd, 2008 at 01:04 PM.
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Previous Instances Remaining

    Yet again, please use code tags when posting code.

    That's a good reason for using _QueryUnload, but not for putting your actual unload code (releasing objects etc) in it rather than in _Unload.

    The point I tried to make (that RhinoBull partly quoted), and admittedly didn't explain clearly, is that the _QueryUnload event should just be used to determine if the unload should be cancelled or not (as in the snippet you posted), and the _Unload event is where the your unloading code should go.

  13. #13
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: [RESOLVED] Previous Instances Remaining

    Quote Originally Posted by si_the_geek
    Yet again, please use code tags when posting code.

    That's a good reason for using _QueryUnload, but not for putting your actual unload code (releasing objects etc) in it rather than in _Unload.

    The point I tried to make (that RhinoBull partly quoted), and admittedly didn't explain clearly, is that the _QueryUnload event should just be used to determine if the unload should be cancelled or not (as in the snippet you posted), and the _Unload event is where the your unloading code should go.
    How do you use code tags?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Previous Instances Remaining

    You can either select the code and then press the Code/VBCode button in the post editor screen (or at the top of the Quick Reply box), or by put them in manually, like this:
    [code] code here [/code]

    If you want to try it out, post a new thread in our Test area.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width