Prevent a dot.net app to start a second instance

T

Totto

Hi
How do I prevent a program from beeing started more than one time.
IE. If a dot net app is already started how do I prevent a second instance
to be started ?

Tnx
Totto
 
L

Leon Chi

Try this:

If
UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
Return
End If

It is a VB.NET syntax.

Leon
 
J

Jon Skeet [C# MVP]

Leon Chi said:
Try this:

If
UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
Return
End If

It is a VB.NET syntax.

However, that fails if your app happens to have the same process name
as another app. The mutex approach as specified in the link I posted
(and which is very easy to port to VB.NET if needed) allows a virtually
arbitrary string for the name of the mutex, which is a lot less likely
to collide with a mutex from a completely unrelated app.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top