Limiting an exe to one instance

  • Thread starter Thread starter A Dude
  • Start date Start date
A

A Dude

Is there an easy way to limit an exe in a directory from
executing more than one instance?
 
Hello,

A Dude said:
Is there an easy way to limit an exe in a directory from
executing more than one instance?

\\\
Public Shared Function PrevInstance() As Process
Dim c As Process = Process.GetCurrentProcess()
Dim p As Process
For Each p In Process.GetProcessesByName(c.ProcessName)
If p.Id <> c.Id Then
If p.MainModule.FileName = c.MainModule.FileName Then
Return p
End If
End If
Next p
Return Nothing
End Function
///
 
There are a couple of things you can do.

1) The easiet way to do it is to use the System.Diagnostics namespace.
You can call a static method to get an array of Process objects given
a name. If the list comes back with more than zero elements, then the
application is running. Here is the sample:

System.Diagnostics.Process[] procs =
System.Diagnostics.Process.GetProcessesByName("MyAppName");

Note that the application name does not include the exe extention.

2) When the application comes up bind to a port on the local machine.
If it is not able to bind to a port, that means that it is already up.
At that point the application should exit. The assumption you have
hard coded a port that is not well known.

3) If possible you can take advantage of COM+. COM+ can manage object
counts. Have a look at it. I will not say anymore here because I think
the first option will work for you.

sayed
 
Back
Top