I want to ask the correct way to use Mutex so that external apps (Notepad) can be opened once. Below are the codes that I am trying right now.
Imports System.Threading
Public Class Form1
Dim proc As New System.Diagnostics.Process()
Public Function IsSingleInstance() As Boolean
Try
Mutex.OpenExisting("Mutex")
Catch
mutexx = New Mutex(True, "Mutex")
Return True
End Try
Return False
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
proc = Process.Start("notepad.exe")
proc.WaitForInputIdle()
Me.Activate()
If Not IsSingleInstance() Then
MsgBox("There are more instance!")
proc.CloseMainWindow()
Else
IsSingleInstance()
End If
End Sub
End Class
The problem with my code is that after I close all notepad windows and click button1 again, notepad cannot be opened. I hope that maybe you guys can share ideas on solving this problem. Thank you.