Avoiding Multiple Instances of a VB program

R

Thread Starter

RaghuDevi

I write software for control applications in VB6. Some times the users start another instance of the application when the first one is minimized, by mistake.

How to avoid this from happening? When sommeone tries to do this, he should get a warning dialog box "Application already running" and stop loading the second instance.

Is there any API function for this?

Thanks

RaghuDevi
 
In the C++ world we use a "named mutex" usually. I haven't used enough of VB to know how to use them, other than creating VB-callable routines to access them.

Essentially the first thing the program does upon launch is create a named mutex and reserve it. When the next instance of your app tries to create it, it will fail. It should terminate at this point, or better, send a message to the first app, then terminate, allowing the first app to maximize or otherwise come to the top. Or, as in the case you suggest, issue a warning box, then terminate.

I would suggest you do the infamous Googling for "VB Named Mutex"

<time passes...>

I just did, and this was the top hit: <A HREF="http://www.mentalis.org/apilist/CreateMutex.shtml">http://www.mentalis.org/apilist/CreateMutex.shtml</A>

There's even a link to example: Advanced PrevInstance

Which is what you're looking for.

Rufus

P.S. Here's a link to a C++ example: <A HREF="http://www.codeguru.com/forum/showthread.php?threadid=231178"> http://www.codeguru.com/forum/showthread.php?threadid=231178</A>

Rufus V. Smith
(Still unemployed)
[email protected]
http://members.aol.com/rufusvs
 
S

Sandeep Shroff

give a search on google for "single program instance" of VB. You will find a lot of examples for the same.

Sandeep
 
T
I haven't tried this yet, but try having you application startup, look for the existence of a file, if not, create it, then delete it when the application closes. If the file is not found, display an error message then close. Make sure your error message tells the user where the temp
file is located in case the application crashes.

--
C. Thomas Wiesen
[email protected]
 
<pre>
If App.PrevInstance = true Then
MsgBox "Application already running", vbOKOnly
End
End If
</pre>
<p>Joe Manns<br>
Arden Environmental Engineering Inc.<br>
3550 Lexington Ave. N<br>
Shoreview, MN 55126<br>
Ph. 651-484-5415<br>
Fax. 651-484-5568<br>
 
B
Place this code in your main start-up subroutine:

If App.PrevInstance Then End

It will do the job - good luck

Bryan Steer
 
Since VB3 there has been a property known as object.AppPrevInstance which is set to TRUE if an older instance of the program already exists. You may wish to review MS KB article Q102480.

Hope this helps.
 
B

Brian E Boothe

<p>Yes Grasshoppa there is a way; hehe
<pre>
------------------(cut)-------------------------------------------------
------
' Declarations --
Public Const GW_HWNDPREV = 3

Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) _
As Long
Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long

' CODE
Private Sub Form_Load()
If App.PrevInstance Then
ActivatePrevInstance
End If
End Sub

Sub ActivatePrevInstance()
Dim OldTitle As String
Dim PrevHndl As Long
Dim result As Long

'Save the title of the application.
OldTitle = App.Title

'Rename the title of this application so FindWindow
'will not find this application instance.
App.Title = "unwanted instance"

'Attempt to get window handle using VB4 class name.
PrevHndl = FindWindow("ThunderRTMain", OldTitle)

'Check for no success.
If PrevHndl = 0 Then
'Attempt to get window handle using VB5 class name.
PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
End If

'Check if found
If PrevHndl = 0 Then
'Attempt to get window handle using VB6 class name
PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
End If

'Check if found
If PrevHndl = 0 Then
'No previous instance found.
Exit Sub
End If

'Get handle to previous window.
PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)

'Restore the program.
result = OpenIcon(PrevHndl)

'Activate the application.
result = SetForegroundWindow(PrevHndl)

'End the application.
End
End Sub
</pre>
 
F

Frank Prendergast

Dear RaghuDevi

Even easier than that.

Built into at least V6.0 of VB is the App object that has a method called PreviousInstance.

So if App.PreviousInstance is true someone is trying to have multiple instances of the program open so have your code shut down.

Check the online help for more info.
 
<p>Here is how you do it for C/C++. The key API is FindWindow(). Perhaps you can translate this to VB.
<pre>
PrevHwnd = FindWindow(AppClass,NULL);
if(PrevHwnd==NULL)//First instance only:
{
if(ProgramInit(nCmdShow)==FALSE)
return FALSE;
}
else // Already is an instance running...
{
BringWindowToTop(PrevHwnd);
ShowWindow( PrevHwnd, nCmdShow );
return FALSE;// (don't start this instance)
}
</pre>
 
<p>In Form_load or main subroutine, add the following code:
<pre>
If App.PrevInstance Then
MsgBox ("The Programme is already running!"), vbExclamation
Unload Me
Exit Sub
End If
</pre>
<p>It's ok
 
C
This has been simply handled since the early days of computing with a lock file. Simply have your program look for the lockfile and abort if there is one. If not, it creates one and this locks out other instances of the same program. It should of course, remove the lockfile on exit. Of course this also depends on your program not crashing and leaving a lockfile, but this can be handled by deleting any lockfiles on reboot. Most UNIX apps write a process number in the lockfile so the owner can be determined but I'm not sure how that applies to Windows.

Regards

cww
 
L

Lynn at Alist

As many people have pointed out, there are VB attribs existing to do just this. I'd suggest you browse a few books on VB as MANY include an example of exactly what you want!

Or do a web serahc on the ".appprevious method" or "App.PrevInstance" as your desire is almost a universal one. Few serious VB apps expect to have multiple instances running concurrently.

- LynnL
 
M
Hi RaghuDevi,

I want to know how can I find if my other vb application is not running then start that application through VB Service application?

-Thanks
-Mamta
 
C

Curt Wuollet

My highly refined solution for this problem is to avoid ever having a first instance of a VB program running on my machines. I've found this to be extremely effective.

Regards

cww
 
Top