PC Review


Reply
Thread Tools Rate Thread

Diagnostics.Process.GetCurrentProcess hangs

 
 
=?Utf-8?B?TSBGZXJkaW5hbmRl?=
Guest
Posts: n/a
 
      8th Mar 2004
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
 
Reply With Quote
 
 
 
 
Anand Balasubramanian
Guest
Posts: n/a
 
      9th Mar 2004
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/de...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

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      9th Mar 2004
* (E-Mail 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
///

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error after calling System.Diagnostics.Process.GetCurrentProcess().StartTime Matthew Lock Microsoft C# .NET 1 21st Feb 2007 10:29 AM
what permissions does a windows service need to execute another process? System.Diagnostics.Process process = System.Diagnostics.Process.Start(info); just local administrator? any specific permitions? Daniel Microsoft Windows 2000 Networking 1 13th Apr 2006 08:58 AM
what permissions does a windows service need to execute another process? System.Diagnostics.Process process = System.Diagnostics.Process.Start(info); just local administrator? any specific permitions? Daniel Microsoft Windows 2000 Security 0 12th Apr 2006 11:47 PM
Application hang calling System.Diagnostics.Process.GetCurrentProcess() Heiko Weiss Microsoft Dot NET Framework 1 20th Jan 2005 03:32 AM
System.Diagnostics.Process.GetCurrentProcess().ProcessName =?Utf-8?B?Ry5Sb2VsYW50?= Windows XP Embedded 1 17th Oct 2004 10:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:25 AM.