|
-
Jan 30th, 2008, 03:30 PM
#1
Thread Starter
Member
-
Jan 30th, 2008, 03:42 PM
#2
Re: Previous Instances Remaining
 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 ...
-
Jan 30th, 2008, 03:51 PM
#3
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).
-
Jan 30th, 2008, 03:53 PM
#4
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
-
Jan 30th, 2008, 03:53 PM
#5
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
-
Feb 1st, 2008, 02:40 PM
#6
Thread Starter
Member
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.
-
Feb 1st, 2008, 02:53 PM
#7
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).
-
Feb 1st, 2008, 02:59 PM
#8
Re: [RESOLVED] Previous Instances Remaining
 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.
-
Feb 1st, 2008, 03:15 PM
#9
Re: [RESOLVED] Previous Instances Remaining
 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 
-
Feb 2nd, 2008, 08:38 AM
#10
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?
-
Feb 2nd, 2008, 12:56 PM
#11
Re: [RESOLVED] Previous Instances Remaining
 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 
-
Feb 2nd, 2008, 01:34 PM
#12
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.
-
Feb 2nd, 2008, 01:57 PM
#13
Re: [RESOLVED] Previous Instances Remaining
 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 
-
Feb 2nd, 2008, 02:01 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|