“make single instance application” what does this do?

后端 未结 7 2007
后悔当初
后悔当初 2020-12-19 02:58

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 instanc

7条回答
  •  时光取名叫无心
    2020-12-19 03:35

    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.

提交回复
热议问题