If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

It wont also....

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.

Re: It wont also....

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...

Re: It wont also....

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

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 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.