When If App.PrevInstance doesn't detect a previous instance
I've found this code in this forum
Private sub Form_Load()
'see if there is an istance already
If App.PrevInstance Then
msgbox "Application is already running"
AppActivate App.Title
'send the key (here SHIFT key) to set the form from the Prev Instance to the top
SendKeys "+", True
'Terminate the new instance
Unload Me
End if
End sub
However, It doesn't appear to work for me.
(I don't care about the SendKeys() and AppActivate() calls, so I'm down to this)
Private sub Form_Load()
'see if there is an istance already
If App.PrevInstance Then
'Terminate the new instance
Unload Me
End if
End sub
I've tried running this
If App.PrevInstance Then
'Terminate the new instance
Exit Sub
End if
as the first line of code in my main() routine as well, and it just does not detect another instance of the program running.
If you make a copy of the exe in a different folder, and launch the two copy of same exe, they will not know of each other)
Or even if you make a copy of exe in same foder (with a different filename name) and run, they will not know they are the same...
Last edited by Cimperiali; May 5th, 2004 at 03:55 AM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Also keep in mind that Windows NT can support multiple desktops and if you use an exe designed to work with distributed COM, even more funky stuff can occur.
Originally posted by Cimperiali
If you make a copy of the exe in a different folder, and launch the two copy of same exe, they will not know of each other)
Or even if you make a copy of exe in same foder (with a different filename name) and run, they will not know they are the same...
So what needs to be done to make this mechanism work ?
Originally posted by hendramin
did you put the code in sub main ? have you set the startup object to sub main ? there is no problem with app.previnstance.
As posted in my original message, I have tried this as the first few lines in main(), as well as in form_load() of the first form which is loaded.
No matter what I've tried, it doens't seem to work- so obviously, I'm missing something crucial :-)
Originally posted by hendramin
did you put the code in sub main ? have you set the startup object to sub main ? there is no problem with app.previnstance.
Hmm. I'm not sure how to explicitly set the "startup object to sub main", but main() is called before any of the formas (as verified with a breakpoint). Maybe this is the default??
How does one explicitly set main() to be called as the startup objec t?
Originally posted by Cimperiali
If you make a copy of the exe in a different folder, and launch the two copy of same exe, they will not know of each other)
Or even if you make a copy of exe in same foder (with a different filename name) and run, they will not know they are the same...
I did not realize this before !!
seems that PrevInstance checks if the EXE module is loaded or not..
So this does not work if you make copies of the EXE
anyway this is my solution for this prolem :
clsMutex Code :
Code:
Option Explicit
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const ERROR_ALREADY_EXISTS = 183&
Private m_hMutex As Long
Public Function CheckMutex(MutexName As String) As Boolean
Dim s As SECURITY_ATTRIBUTES
m_hMutex = CreateMutex(s, 0, MutexName)
If Err.LastDllError = ERROR_ALREADY_EXISTS Then
CheckMutex = False
Else
CheckMutex = True
End If
End Function
Private Sub Class_Terminate()
CloseHandle m_hMutex
End Sub
Form code:
Code:
Option Explicit
Dim mut As clsMutex
Private Sub Form_Load()
Set mut = New clsMutex
If Not mut.CheckMutex("CodeGuru") Then 'Case sensitive string
MsgBox "Application is running"
Unload Me
End If
End Sub
How does it work ?
it creates a mutex (shared object that hace a uniqe name)..
if you try to recreate it you get the error :ERROR_ALREADY_EXISTS
this is the whole idea..But remember that mut (the clsMutex instance) must be alive as long as you application is still running because the mutex is close in the terminate event.
try it
Originally posted by cappy2112
but then I compiled 2 identical EXE's, with different filenames.
App.PrevInstance will look to see if the actual application is already running - not whether you have another exe running at the same time. ie - 2 copies of the same program running.
Originally posted by Twodogs
App.PrevInstance will look to see if the actual application is already running - not whether you have another exe running at the same time. ie - 2 copies of the same program running.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.