|
-
May 30th, 2005, 05:16 AM
#1
Thread Starter
Lively Member
Preventing more than one instance of app running
Hi there
Is there any way to prevent more than one instance of one's application to run at the same time? Is there any way to check when the application loads if an instance of the app is already running and if it is then don't load the application?
Microsoft seems to do this with Office applications.
Many thanks
langals
-
May 30th, 2005, 05:20 AM
#2
Re: Preventing more than one instance of app running
In your SubMain or Form_Load....
VB Code:
If App.PrevInstance = True Then
MsgBox "Application is already running..."
End
End If
-
May 30th, 2005, 05:22 AM
#3
Re: Preventing more than one instance of app running
 Originally Posted by langals
Hi there
Is there any way to prevent more than one instance of one's application to run at the same time? Is there any way to check when the application loads if an instance of the app is already running and if it is then don't load the application?
Microsoft seems to do this with Office applications.
Many thanks
langals
Try something like this:
VB Code:
Private Sub Form_Load()
Dim frmMyForm As Form
If App.PrevInstance = True Then
For Each frmMyForm In Forms
Unload frmMyForm
Set frmMyForm = Nothing
Next frmMyForm
Set frmMyForm = Nothing
End
End If
End Sub
Cheers,
RyanJ
-
May 30th, 2005, 05:26 AM
#4
Re: Preventing more than one instance of app running
 Originally Posted by sciguyryan
Private Sub Form_Load()
Dim frmMyForm As Form
If App.PrevInstance = True Then
For Each frmMyForm In Forms
Unload frmMyForm
Set frmMyForm = Nothing
Next frmMyForm
Set frmMyForm = Nothing
End
End If
End Sub
I think the bold ones are unnecessary since the app is still loading, thus I assume no form has been loaded yet......
-
May 30th, 2005, 05:30 AM
#5
Re: Preventing more than one instance of app running
 Originally Posted by dee-u
I think the bold ones are unnecessary since the app is still loading, thus I assume no form has been loaded yet......
Probably but I always leave them there just to be shure. Unless you are looking for a very snappy shutdown (Which that code will not affect by much in any case) it should work all the same 
Cheers,
RyanJ
-
May 30th, 2005, 05:38 AM
#6
Re: Preventing more than one instance of app running
-
May 30th, 2005, 05:56 AM
#7
Re: Preventing more than one instance of app running
 Originally Posted by dee-u
Nice use of a mutex there. I'd say that's probably the best way to do it, out of the ways I've seen so far (registry flag etc.).
If your app is one that loads data (like a document reader), just quitting if there is an existing instance probably isn't enough. Every time you open a Word document from Explorer, you are opening an instance of Word. If there is already an instance, it sends the document path back to the original instance before it quits, otherwise the document would never get opened. There are a variety of ways you can do this. One way (probably the simplest) is shown in the "Sending strings between VB apps" link in my sig.
-
May 30th, 2005, 06:14 AM
#8
Thread Starter
Lively Member
Re: Preventing more than one instance of app running
Thanks for the help guys - that did the trick.
langals
-
May 30th, 2005, 07:45 PM
#9
Re: Preventing more than one instance of app running
This alternative might be noteworthy too....
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Sub Main()
Dim m_hWnd As Long
'Change Form1 to your form Caption.
m_hWnd = FindWindow(vbNullString, "Form1")
If m_hWnd > 0 Then
MsgBox "Program " & App.Title & " is already running..."
Unload Form1
Exit Sub
End If
'Change Form1 to your form name.
Form1.Show
End Sub
-
Nov 8th, 2005, 08:15 PM
#10
PowerPoster
Re: Preventing more than one instance of app running
 Originally Posted by dee-u
This alternative might be noteworthy too....
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Sub Main()
Dim m_hWnd As Long
'Change Form1 to your form Caption.
m_hWnd = FindWindow(vbNullString, "Form1")
If m_hWnd > 0 Then
MsgBox "Program " & App.Title & " is already running..."
Unload Form1
Exit Sub
End If
'Change Form1 to your form name.
Form1.Show
End Sub
I am trying to use your code with a login form, the prompt comes up to say its already loaded but it also logs you in without credentials... ugh.
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Sub Main()
Dim m_hWnd As Long
'Change Form1 to your form Caption.
m_hWnd = FindWindow(vbNullString, "Login to Client Manager Pro")
If m_hWnd > 0 Then
frmAppAlreadyRunning.Show vbModal
Unload frmLoginForm
Exit Sub
Else
frmLoginForm.Show
End If
End Sub
-
Nov 8th, 2005, 08:42 PM
#11
Re: Preventing more than one instance of app running
Put it in sub Main before the frmLogin ever gets called.
-
Nov 8th, 2005, 09:21 PM
#12
PowerPoster
Re: Preventing more than one instance of app running
 Originally Posted by dglienna
Put it in sub Main before the frmLogin ever gets called.
tried that too and when you click on the desktop icon. nothing loads, nothing checks, nothing happens.
-
Nov 8th, 2005, 09:30 PM
#13
Re: Preventing more than one instance of app running
This only runs the code if it's registered, and doesn't allow it to be executed twice. It is a case that you have to use END.
Post the code that you have. Sounds like the app is still running in the background.
VB Code:
Public Sub Main()
If App.PrevInstance Then
Beep
End
Else
GetAppEnvironment
If Not gRegClass.Registered Then
frmRegister.Show vbModal
End
Else
frmBackground.Show
End If
End If
End Sub
-
Nov 8th, 2005, 11:30 PM
#14
PowerPoster
Re: Preventing more than one instance of app running
 Originally Posted by dglienna
This only runs the code if it's registered, and doesn't allow it to be executed twice. It is a case that you have to use END.
Post the code that you have. Sounds like the app is still running in the background.
VB Code:
Public Sub Main()
If App.PrevInstance Then
Beep
End
Else
GetAppEnvironment
If Not gRegClass.Registered Then
frmRegister.Show vbModal
End
Else
frmBackground.Show
End If
End If
End Sub
ill try it and get back to you
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
|