I spent a few minutes reading through some ways people have done this online, but it seems to boil down to searching through the processes and finding one that matches your form's Title or something such. I ended writing a quick method that searches on the type of the form in the AppMain's InitApplication method.
Is this the best way to handle this ? Also this doesn't currently allow the same application to be run by multiple users on the same system, ie users over Terminal Services etc. I guess I would have to add another quick check for if the Owner of that Process is the current Application's process owner or some such.
I was just wanting to get some input from anyone who may have had to do this as well.
Thanks
Robin Giltner
I've been using the mutex method for restricting an application to a single instance. It's easy to implement and so far it's worked well for me. By varying the safeName you should be able to over come the problem in Terminal Services.
I include the following code at the top of Public Shared Sub Main in AppMain.
Dim firstInstance As Boolean = False'-- Create a name based on the user application path. Dim safeName As String = Application.UserAppDataPath.Replace("\", "_")Dim myMutex As Mutex = New Mutex(True, safeName, firstInstance)'-- Bail if not first instance.If Not (firstInstance) Then Exit Sub'-- Make sure the program holds the mutex as long as it is running.GC.KeepAlive(myMutex)
See http://www.ai.uga.edu/mc/SingleInstance.html for more info.
-Larry
Thanks for the code snippet. I was just trying to accomplish this, and it worked really fine.