Diagnostics.Process.GetCurrentProcess hangs

G

Guest

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.InteropService

Public Class Global

<DllImport("user32.dll")> Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Boolea
End Functio
<DllImport("User32.dll")> Private Shared Function GetWindowLong(ByVal HWND As IntPtr, ByVal Index As Integer) As Intege
End Functio

Private Const SW_RESTORE As Integer =
Private Const GWL_STYLE As Integer = -1
Private Const WS_MINIMIZE As Integer = &H2000000

Public Shared Sub Main(

Dim PrevProcessID As Integer = PrevInstance(
If PrevProcessID = -1 The
Application.Run(New Form1
Els

AppActivate(PrevProcessID
End I

End Su

Private Shared Function PrevInstance() As Intege
Dim myProcesses As Process(
Dim localProcess As Process = Diagnostics.Process.GetCurrentProces
Dim procName As String = localProcess.ProcessNam
myProcesses = Diagnostics.Process.GetProcessesByName(procName
If UBound(myProcesses) > 0 The
Dim otherProcess As Proces
If myProcesses(0).Id = Process.GetCurrentProcess.Id The
otherProcess = myProcesses(1
Els
otherProcess = myProcesses(0
End I
Dim result As Intege
result = GetWindowLong(otherProcess.MainWindowHandle, GWL_STYLE
If (result And WS_MINIMIZE) = WS_MINIMIZE The
ShowWindow(otherProcess.MainWindowHandle, SW_RESTORE
End I
Return otherProcess.I
Els
Return -
End I
End Functio

End Class
 
A

Anand Balasubramanian

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
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (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
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top