Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allow only one instance of an application to run? 4

Status
Not open for further replies.

vbkris

Programmer
Jan 20, 2003
5,994
IN
I have created an exe using VB. i want only one instance of the application to run at a given point. but by default all exe's created in VB can always have more than one instance. i.e
double click on the exe file an instance is created.
while the exe is running if i double click on the exe file again a new instance is created. i want the original instance to automatically popup (like dreamweaver, adobe)
 
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Put this in your module, and your app should start with sub main, not a form...

Public Sub Main()
Dim frmHwnd As Long


frmHwnd = FindWindow(vbNullString, "yourformnamehere")
If m_hWnd > 0 Then
MsgBox App.Title & " is currently running..."
Unload yourformnamehere
Exit Sub
End If
yourformnamehere.Show ' this fires if yourformname isn't already running
End Sub
Tuna - It's fat free until you add the Mayo!
 
hmmm... Is there a full listing anywhere for User32???
that DLL (and GDI32) have all the cool stuff in them... Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
hey I fudged a bit on this one...
replace
[If m_hWnd > 0 Then]
with
if frmHwnd > 0 then

I grabbed that code from another project and changed the variable to make it a bit easier to follow and missed that blasted variable... sorry. Tuna - It's fat free until you add the Mayo!
 
this is the code i placed in the main
form name is Form1
code did not work
Dim frmHwnd As Long
frmHwnd = FindWindow(vbNullString, "Form1")
If frmHwnd > 0 Then
MsgBox App.Title & " is currently running..."
Exit Sub
End If
Form1.Show
 
thanks but creating a single instance only is not there in that page.
 
You missed the UNLOAD!

Unload yourformnamehere

and you could toss an END after the unload...



Tuna - It's fat free until you add the Mayo!
 
I have a new problem.
the code u gave stops a new instance.
i want a code that will activate the original instance automatically.
 
add
AppActivate "form1",0

after the unload form1


Tuna - It's fat free until you add the Mayo!
 
You can skip the message box all together if you would like... you don't have to tell the user anything, just call the AppActivate and viola, you're done!

Tuna - It's fat free until you add the Mayo!
 
another problem
i am using an MDI form.the title changes from child form to child form. therefore the mdi title also changes. is there a code that doesnt use the title but some other identity like form name?
 
Oh, that's different. The findwindow function returns your window handle based on the "name", ie the caption... so if the caption changes when a different child window is on display, you're out of luck with findwindow.

Or maybe not... I've got to get one of my books out of the car, I'll get back to you in just a bit.

Tuna - It's fat free until you add the Mayo!
 
ok, tell me what some of the possible window names are for your app so I can see what's happening.

does the window name become the child window name, or is the windowname partially the same, with the child window name appended to the main window name

ie. tunaform
tunaform - tunachildform Tuna - It's fat free until you add the Mayo!
 
some examples

Welcome - Add Shift
Welcome - Modify Shift
Welcome - Delete Shift

Welcome - Add Worker
Welcome - Modify Worker
Welcome - Delete Worker
 
I am logging out for half hour. will be back
 
Ahh, see if it will find your form using "welcome"

frmHwnd = findwindow(vbNullString, "Welcome -")

see what happens and let me know! Tuna - It's fat free until you add the Mayo!
 
This is the easiest way to prevent a second instance from running;

in the FORM_LOAD add;

If (App.PrevInstance = True) Then
MsgBox &quot;<yourname>.exe already started!&quot;, vbOKOnly + vbExclamation, &quot;Startup Error&quot;
End
End If
 
I found some pre-written code to COUNT the number of instances of a given application. Modify the code to your needs.

put this code in your sub main() and if the return value is not zero, end the application and call appactivate &quot;Window -&quot;,0 as before

here's the link:


Hope this helps, I've got to crash! 3 hours until I have to get up and take kids to school... :-(
Tuna - It's fat free until you add the Mayo!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top