Link to home
Start Free TrialLog in
Avatar of T Hoecherl
T HoecherlFlag for United States of America

asked on

Check whether a vb.net program is already running

When a user attempts to launch a vb.net program, I would like to check to see if anyone else I already running the program and, if so, display an error message and abort the launch.  I assume this would be done in the Load event.  How can I do this using vb.net code?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

set the IsSingleInstance to true and write an event on

me.IsSingleInstance = true

Open in new window


WindowsFormsApplicationBase.IsSingleInstance Property
https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.onstartupnextinstance(v=vs.110).aspx

to bring the running  instance to front, or give a message and exit...

Protected Overrides Sub OnStartupNextInstance(e As StartupNextInstanceEventArgs)
	MyBase.OnStartupNextInstance(e)

	' Dim secondInstanceArgumens As String() = e.CommandLine.ToArray()
	' Handle command line arguments of second instance

	If e.BringToForeground Then
		Me.MainForm.BringToFront()
	End If
End Sub

Open in new window

Avatar of T Hoecherl

ASKER

I also need to display the userid or username of whoever is already running the application.  Can this be done?
Here is what I need to do, in the final analysis:  if a user attempts to launch the program and another user is already running the program, I need to display a message which says "Only one instance is allowed.  Program is currently being run by " & userid.  Then I need to exit the program for the second user.
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
any reason why you have this limitation? if someone opens the application and leaves for lunch, nobody else will be able to use the app!
yes, but app will tell who is using the app, so they can call him and get the user password, login to his machine and shut down the app :)
if the guy is working remotely through VPN or has left for lunch or on vacation and the computer is locked, it will not be easy to kick him out
Can't they use Task Manager to kick him out?
a small unlock.exe can handle the situation :)
but if the computer is locked down, you won't be able to access the task manager
What would the unlock.exe look like?  The executable that launches the program is named JPG_PRODUCTION_BATCHING.exe.
unlock.exe can delete the record from database or delete the lock file so another one can run the same app...

but this requires main app check this file on focus, and if it is deleted/modified then exits with a message "oops, you are kicked off!"
I know how to write code to delete the record from the database, but I don't know how to cause the currently running program to exit.
I know how to write code to delete the record from the database, but I don't know how to cause the currently running program to exit.

Open in new window


too much trouble here, but I suggest use a timer that runs every minute and check the file or db

if it is kicked off (db is reset or file is deleted or file contains someone else) then exit silently, or keep a log in db and exit...

so, if someone opens the app and goes to sleep,
you run unlock exe...
then the app detects this in 1 min and exit
and someone else can run it...
HainKurt,

I still don't get how the unlock.exe will work.  I have it working now so that when a user logs in an entry is made in a sql table indicating user name and time logged in.  If a second user tries to log in, the table is checked and if there is an entry there, the 2nd user gets a message telling him who is currently logged in and the launch is aborted for the second user.

I also have a second program that a user can click, which will remove the entry from the sql table and, hence allow log in.  This is so if the first user left his application running and left the building, the second user can clear the table and log in.  But I still need a way to cause the application for the first user to exit.
ok, all good... but if you can add this to your main app, it will be much better...

* every minute (and/or on focus) check that table to see if we are still the only user...
* if not (kicked off), just exit the app :)

current way, lots of app can run at the same time...

I suggest you use a session table like

session
- sessionid (GUID) PK (it may also be a integer)
- userid
- machinename
- IP
- logindatetime
- status
- logoutdatetime

and do not delete anything, but just insert/update...
- insert a record when a user logs in
- check the status every minute (maybe we are kicked off, so exit immediately)
- update on logout
- update on kick off