10

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

7 Answers7

20

does it make it so that it's impossible to open two instances at the same time?

Yes.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
14

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.

Steve
  • 5,585
  • 2
  • 18
  • 32
11

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.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
3
    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
R.Alonso
  • 989
  • 1
  • 8
  • 9
2

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
spenibus
  • 4,339
  • 11
  • 26
  • 35
Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
1

Yes you're correct in that it will only allow one instance of your application to be open at a time.

Web
  • 1,735
  • 2
  • 22
  • 36
-3

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.

Ahmed Fwela
  • 955
  • 1
  • 13
  • 32
  • 2
    Requires 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
  • 24
    I would **not** say this is easier than ticking the `Make Single Instance Application` checkbox – Matt Wilko Apr 29 '14 at 08:49