Imports System.Runtime.InteropServices
Public Class Globals
<DllImport("user32.dll")> Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
End Function
<DllImport("User32.dll")> Private Shared Function GetWindowLong(ByVal HWND As IntPtr, ByVal Index As Integer) As Integer
End Function
Private Const SW_RESTORE As Integer = 9
Private Const GWL_STYLE As Integer = -16
Private Const WS_MINIMIZE As Integer = &H20000000
Public Shared Sub Main()
Dim PrevProcessID As Integer = PrevInstance()
If PrevProcessID = -1 Then
Application.Run(New Form1)
Else
AppActivate(PrevProcessID)
End If
End Sub
Private Shared Function PrevInstance() As Integer
Dim myProcesses As Process()
Dim localProcess As Process = Diagnostics.Process.GetCurrentProcess
Dim procName As String = localProcess.ProcessName
myProcesses = Diagnostics.Process.GetProcessesByName(procName)
If UBound(myProcesses) > 0 Then
Dim otherProcess As Process
If myProcesses(0).Id = Process.GetCurrentProcess.Id Then
otherProcess = myProcesses(1)
Else
otherProcess = myProcesses(0)
End If
Dim result As Integer
result = GetWindowLong(otherProcess.MainWindowHandle, GWL_STYLE)
If (result And WS_MINIMIZE) = WS_MINIMIZE Then
ShowWindow(otherProcess.MainWindowHandle, SW_RESTORE)
End If
Return otherProcess.Id
Else
Return -1
End If
End Function
End Class
imports system.threading
Dim bl As Boolean
Dim m As New Mutex(True, "Hello", bl)
If (bl) then
'Mutex created
else
'mutex with the same name as already been created and so the app has
already started
end if
checkout the following link for more information on mutex
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemthreadingwaithandleclasshandletopic.asp
Anand Balasubramanian
Microsoft, Visual Basic .NET
This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks
I fully agree, nevertheless I know at least about one machine where the
code above /didn't/ work (for some unknown reason). Nevertheless, this
code worked:
\\\
Dim m As Mutex = _
New Mutex(False, "{11C92606-65D9-4df2-9AEA-B6A4DA91BCE2}")
If m.WaitOne(10, False) Then
Application.Run(New Form1())
m.ReleaseMutex()
Else
MessageBox.Show("Application already running!")
End If
///
--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>