How can I tell if the program is already running?
I need to make sure that there is ONE only ONE instance of my app is running
App.PrevInstance only checks for previous instances within the current session.
I need to make sure that there is only one instance across ALL users.
Listing processes in the TaskManager might be an option but are there any other methods?
I thought about settting a flag in a common file which clears when the app stops, but I don't want to do that because it might not get cleared if the program exits ubnormally.
Re: How can I tell if the program is already running?
Well, I'm not much of a help here, but if your app has an open window you could use an api to find it.
Re: How can I tell if the program is already running?
Heres the code to check to see if the app is running
WinWnd = FindWindow(vbNullString, PROGRAM_WINDOW)
' PROGRAM_WINDOW is App Name ie. MyApp.exe
If WinWnd = 0 Then
'program is not running
else
'program is running
end if
Re: How can I tell if the program is already running?
Well I don't think that's going to work because the program might be running under someone else's login.
Re: How can I tell if the program is already running?
Is it the same machine, or different machines?
Re: How can I tell if the program is already running?
Quote:
Originally Posted by Guru
Well I don't think that's going to work because the program might be running under someone else's login.
You mean like under Windows XP Fast User Switching?
Re: How can I tell if the program is already running?
I think you'd need to implement some sort of in-use registry(?) flag on a centrally located PC or server and then have the program check that flag to see if the app is in use.
Re: How can I tell if the program is already running?
BTW why do you need to restrict it to one user at a time? Does it use some sort of common database or file?
Re: How can I tell if the program is already running?
I find this to be more versatile than App.PrevInstance.
IE: not only will it check for previous running instances but will bring the app to the foreground if it had been minimized.
You can easily modify it to do other stuff besides just that. :)
Please compile before running ;)
Cheers!
VB Code:
'Form code
Option Explicit
Private Sub Form_Load()
Call CheckAppPrevInstance
End Sub
'In a module
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function OpenIcon Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Const GW_HWNDPREV = 3
Public Sub CheckAppPrevInstance()
Dim OldTitle As String
Dim ll_WindowHandle As Long
'saving the current title in OldTitle variable
'and changing the application title so it will not look for itself
OldTitle = App.Title
App.Title = "Dup instance"
'finding the previous instance.
ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle)
'if there is no old instances of your application - exit.
If ll_WindowHandle = 0 Then App.Title = OldTitle: Exit Sub
'Find the window we need to restore
ll_WindowHandle = GetWindow(ll_WindowHandle, GW_HWNDPREV)
'Now restore it
Call OpenIcon(ll_WindowHandle)
'And Bring it to the foreground
Call SetForegroundWindow(ll_WindowHandle)
End
End Sub
Re: How can I tell if the program is already running?
For another user, or on another machine?
Re: How can I tell if the program is already running?
Quote:
Originally Posted by dglienna
For another user, or on another machine?
Sorry can't answer that one right now but will try it out first chance I get. :)
Re: How can I tell if the program is already running?
Or is your app on a server like Terminal server?
Re: How can I tell if the program is already running?
Quote:
Originally Posted by daydee
I find this to be more versatile than App.PrevInstance.
IE: not only will it check for previous running instances but will bring the app to the foreground if it had been minimized.
VB Code:
.
.
.
'finding the previous instance.
ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle)
'if there is no old instances of your application - exit.
.
.
.
Exactly what I was looking for - I was just wanting to change my App.PrevInstance code to actually find and bring the window to the front...
What is the ThunderRT6Main part mean? The OldTitle variable already contains the name of the app - so I'm unclear on Thunder part...
Re: How can I tell if the program is already running?
Thats the window class name for a vb 6 form
Re: How can I tell if the program is already running?
Quote:
Originally Posted by RobDog888
Thats the window class name for a vb 6 form
You've got to be kidding me!
Is that like the "code" name of the product during development back in the 90's??
Re: How can I tell if the program is already running?
Well I have no idea what they were thinking but as windows classnames go its one of the worst. :D
Re: How can I tell if the program is already running?
Quote:
Is that like the "code" name of the product during development back in the 90's??
That is exactly where they got the class name from. RT stands for "run time" and of course 6 is the VB version number. If running out of the IDE you will not use the RT# just Thunder.
Re: How can I tell if the program is already running?
Quote:
Originally Posted by Guru
I thought about settting a flag in a common file which clears when the app stops, but I don't want to do that because it might not get cleared if the program exits ubnormally.
Off the top of my head I can't think of a better way. Also you will want to write a "Safe Shutdown" flag to avoid the situation you mention. If the safe shutdown flag is not set, then ignore the other flag. And reset both at a startup that is deemed to be the first (going on the flag states).
Re: How can I tell if the program is already running?
Quote:
Originally Posted by penagate
Off the top of my head I can't think of a better way. Also you will want to write a "Safe Shutdown" flag to avoid the situation you mention. If the safe shutdown flag is not set, then ignore the other flag. And reset both at a startup that is deemed to be the first (going on the flag states).
But wouldn't the 'safeshutdown' flag be reset to false when the the app starts?
If so, a second instance wouldn't know if the app was running or had crashed...
:confused:
Re: How can I tell if the program is already running?
In answer to earlier replies, the app runs on a remote server.
I wanted to run it as a service but for some reason it wouldn't start. Instead, I added it in the 'run' section of currentversion etc in the registry.
Each time someone logs in remotely a new instance is created which is not what I want
Re: How can I tell if the program is already running?
OK, how about this.
Open your common file when the app starts up, WITHOUT read share permissions, and close it when you stop. That way, if you app starts up and can't open the file, it knows that there is already an instance open. And the file handles are maintained by Windows in memory, so they are not preserved after shutdown.
Your app crashing without Windows shutting down might be an issue however.