in vb 2008 express this option is available under application properties. does anyone know what is its function? does it make it so that it's impossible to open two instances at the same time?
7 Answers
does it make it so that it's impossible to open two instances at the same time?
Yes.

- 363,768
- 54
- 674
- 675
Why not just use a Mutex? This is what MS suggests and I have used it for many-a-years with no issues.
Public Class Form1
Private objMutex As System.Threading.Mutex
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Check to prevent running twice
objMutex = New System.Threading.Mutex(False, "MyApplicationName")
If objMutex.WaitOne(0, False) = False Then
objMutex.Close()
objMutex = Nothing
MessageBox.Show("Another instance is already running!")
End
End If
'If you get to this point it's frist instance
End Sub
End Class
When the form, in this case, closes, the mutex is released and you can open another. This works even if you app crashes.

- 5,585
- 2
- 18
- 32
-
because it's only .NET 4.5. but this is great information indeed! – Alex Gordon Apr 15 '13 at 21:33
-
Whats .NET 4.5? I have been using this since 1.1 and currently use it in VS2008, as stated in the original question. – Steve Apr 16 '13 at 14:07
Yes, it makes it impossible to open two instances at the same time.
However it's very important to be aware of the bugs. With some firewalls, it's impossible to open even one instance - your application crashes at startup! See this excellent article by Bill McCarthy for more details, and a technique for restricting your application to one instance. His technique for communicating the command-line argument from a second instance back to the first instance uses pipes in .NET 3.5.

- 30,070
- 5
- 68
- 111
Dim _process() As Process
_process = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
If _process.Length > 1 Then
MsgBox("El programa ya está ejecutandose.", vbInformation)
End
End If

- 989
- 1
- 8
- 9
-
Cool technique, but your post doesn't really address the OP's question. Consider moving it to a comment. – Erik Knowles Sep 24 '20 at 23:18
I found a great article for this topic: Single Instance Application in VB.NET.
Example usage:
Module ModMain
Private m_Handler As New SingleInstanceHandler()
' You should download codes for SingleInstaceHandler() class from:
' http://www.codeproject.com/Articles/3865/Single-Instance-Application-in-VB-NET
Private m_MainForm As Form
Public Sub Main(ByVal args() As String)
AddHandler m_Handler.StartUpEvent, AddressOf StartUp ' Add the StartUp callback
m_Handler.Run(args)
End Sub
Public Sub StartUp(ByVal sender As Object, ByVal event_args As StartUpEventArgs)
If event_args.NewInstance Then ' This is the first instance, create the main form and addd the child forms
m_MainForm = New Form()
Application.Run(m_MainForm)
Else ' This is coming from another instance
' Your codes and actions for next instances...
End If
End Sub
End Module

- 4,339
- 11
- 26
- 35

- 8,268
- 8
- 31
- 54
Yes you're correct in that it will only allow one instance of your application to be open at a time.

- 1,735
- 2
- 22
- 36
There is even a easier method:
Use the following code...
Imports System.IO
On the main form load event do the following:
If File.Exist(Application.StartupPath & "\abc.txt") Then
'You can change the extension of the file to what ever you desire ex: dll, xyz etc.
MsgBox("Only one Instance of the application is allowed!!!")
Environment.Exit(0)
Else
File.Create(Application.StartupPath & "\abc.txt", 10, Fileoptions.DeleteonClose)
Endif
This will take care of single instances as well as thin clients, and the file cannot be deleted while the application is running. and on closing the application or if the application crashes the file will delete itself.

- 955
- 1
- 13
- 32

- 90
- 7
-
2Requires write-access to the directory containing the EXE, which is not available if you install your EXE under Program Files and you run the EXE as a non-admin user. – MarkJ Oct 11 '13 at 16:11
-
24I would **not** say this is easier than ticking the `Make Single Instance Application` checkbox – Matt Wilko Apr 29 '14 at 08:49