Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Diagnostics.Process.GetCurrentProcess hangs

15 views
Skip to first unread message

M Ferdinande

unread,
Mar 8, 2004, 8:06:07 AM3/8/04
to
Has anyone encountered the following problem.
I' trying to prevent my program starting up twice. In my PrevInstance function I call the function Diagnostics.Process.GetCurrentProcess to retreive the processname. This does not work. This function hangs on my pc. If I try the same code on another PC (both XP) the same code works fine.
I've tried to hard code the processName but then it will hang on the next line
myProcesses = Diagnostics.Process.GetProcessesByName(procName)
(See code below)
Does anyone know what may be the cause and how to solve this problem.


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

Anand Balasubramanian(MSFT)

unread,
Mar 9, 2004, 11:59:06 AM3/9/04
to
Hi ,
The System.Diagnostics namespace uses the performance counters installed
on a machine. If these couters are not installed or if they are corrupt,
you will run into this problem. But your main aim is to find out if a
previous instance of your app is running. The recommended way to do this is
to use the System.Threading.Mutex class and create a named mutex and have
initial ownership. So unless the process that created this mutex releases
it, the other processes cannot have access to it. For example you can have
this code in your startup procedure

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

Herfried K. Wagner [MVP]

unread,
Mar 9, 2004, 1:50:54 PM3/9/04
to
* ana...@online.microsoft.com (Anand Balasubramanian(MSFT)) scripsit:

> The System.Diagnostics namespace uses the performance counters installed
> on a machine. If these couters are not installed or if they are corrupt,
> you will run into this problem. But your main aim is to find out if a
> previous instance of your app is running. The recommended way to do this is
> to use the System.Threading.Mutex class and create a named mutex and have
> initial ownership. So unless the process that created this mutex releases
> it, the other processes cannot have access to it. For example you can have
> this code in your startup procedure
>
> 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

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/>

0 new messages