|
-
Oct 28th, 2005, 06:27 AM
#1
Thread Starter
Addicted Member
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.
Another light-hearted post from Guru 
-
Oct 28th, 2005, 07:47 AM
#2
Fanatic Member
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.
-
Oct 28th, 2005, 09:49 AM
#3
Lively Member
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
-
Oct 28th, 2005, 07:11 PM
#4
Thread Starter
Addicted Member
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.
Another light-hearted post from Guru 
-
Oct 28th, 2005, 07:12 PM
#5
Re: How can I tell if the program is already running?
Is it the same machine, or different machines?
-
Oct 28th, 2005, 08:46 PM
#6
Re: How can I tell if the program is already running?
 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?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 28th, 2005, 09:12 PM
#7
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.
-
Oct 28th, 2005, 09:13 PM
#8
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?
-
Oct 28th, 2005, 11:37 PM
#9
Fanatic Member
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
-
Oct 28th, 2005, 11:40 PM
#10
Re: How can I tell if the program is already running?
For another user, or on another machine?
-
Oct 28th, 2005, 11:55 PM
#11
Fanatic Member
Re: How can I tell if the program is already running?
 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.
-
Oct 29th, 2005, 10:42 AM
#12
Re: How can I tell if the program is already running?
Or is your app on a server like Terminal server?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 29th, 2005, 11:02 AM
#13
Re: How can I tell if the program is already running?
 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...
-
Oct 29th, 2005, 11:13 AM
#14
Re: How can I tell if the program is already running?
Thats the window class name for a vb 6 form
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 29th, 2005, 11:18 AM
#15
Re: How can I tell if the program is already running?
 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??
-
Oct 29th, 2005, 11:22 AM
#16
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 29th, 2005, 12:17 PM
#17
Re: How can I tell if the program is already running?
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.
-
Oct 31st, 2005, 08:19 AM
#18
Re: How can I tell if the program is already running?
 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).
-
Oct 31st, 2005, 11:26 AM
#19
Thread Starter
Addicted Member
Re: How can I tell if the program is already running?
 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...
Another light-hearted post from Guru 
-
Oct 31st, 2005, 11:35 AM
#20
Thread Starter
Addicted Member
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
Another light-hearted post from Guru 
-
Oct 31st, 2005, 11:42 PM
#21
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.
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
|