467,133 Members | 1,138 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,133 developers. It's quick & easy.

Problems with Process Class after WaitforExit

Hi,
well i've a problem and i don't know how to solve it.

I wrote a function, which starts a Process for a user by using the API
CreateProcessWithLogon.

Afterwards i want to know if the process already ended and with what
exitcode it has finished.
So i created an process class object and used the function GetProcessbyID in
order to get more information about the process.

Here's my problem, if the process already finished i can get the property
ExitCode but if i have to wait by using the method waitforexit i get an
error when trying to get the property.

Invalid Operation
The Object hasn't started the process.

Where is the difference between getting the propertie before or after a
threadsleep ?

thanks

christian
Nov 20 '05 #1
  • viewed: 3504
Share:
4 Replies
Hi Christian,
Where is the difference between getting the propertie before or after a
threadsleep ? If you mean call the GetProcessByID before or after a threadsleep, I think
there is no different.
Here's my problem, if the process already finished i can get the property
ExitCode but if i have to wait by using the method waitforexit i get an
error when trying to get the property. If you call the waitforexit , the thread will block, then when will you
call the GetProcessByID to get the process properties.

Can you post your code as simple as possible for me to reproduce the
problem?
I will appreciate your efforts.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------Reply-To: "Christian Billig" <ng****@veda.net>
From: "Christian Billig" <ng****@veda.net>
Subject: Problems with Process Class after WaitforExit
Date: Mon, 10 Nov 2003 14:24:50 +0100
Lines: 28
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <es**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: pns.veda.de 217.6.189.98
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:155505
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi,
well i've a problem and i don't know how to solve it.

I wrote a function, which starts a Process for a user by using the API
CreateProcessWithLogon.

Afterwards i want to know if the process already ended and with what
exitcode it has finished.
So i created an process class object and used the function GetProcessbyID inorder to get more information about the process.

Here's my problem, if the process already finished i can get the property
ExitCode but if i have to wait by using the method waitforexit i get an
error when trying to get the property.

Invalid Operation
The Object hasn't started the process.

Where is the difference between getting the propertie before or after a
threadsleep ?

thanks

christian


Nov 20 '05 #2
Hi Peter,

here's my code :

Public Function BefehlalsUser(ByVal sCMD As String, ByVal sLogonName As
String, ByVal sDomain As String, ByVal sPassWord As String)
Dim i As Int16
sApplicationName = Trim$(sCMD)

i = InStr(sCMD, ":\")
If i <> 0 Then
sDirectory = Mid$(sCMD, 1, 3)
Else
sDirectory = "C:\"
End If
sb = &H0&

sEnviroment = &H0&

startInfo.cb = Marshal.SizeOf(startInfo)
startInfo.dwFlags = 0&
Process_Init_Status = CreateProcessWithLogon(sLogonName, sDomain,
sPassWord, LOGON_WITH_PROFILE, sApplicationName, sb,
CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP,
_

Marshal.StringToBSTR(sEnviroment), sDirectory, startInfo, processInfo)

If (Process_Init_Status = False) Then
Process_Init_ErrorID = Marshal.GetLastWin32Error()
Dim Win32Err As New
System.ComponentModel.Win32Exception(Process_Init_ ErrorID)
Process_Init_Error_MSG = Win32Err.Message
Process_Init_Error_HelpLink = Win32Err.HelpLink
Process_Init_Error_Source = Win32Err.Source
Process_Init_ProcessID = 0
Else
Process_Init_ErrorID = 0
Process_Init_Error_MSG = ""
Process_Init_Error_HelpLink = ""
Process_Init_Error_Source = ""
Process_Init_ProcessID = processInfo.dwProcessId
End If

End Function

Public Function Process_Trace() As Boolean
'Prozess verfolgen
'Bei Beendigung muß der ExitCode abgefragt werden
'Erste Schritt Prozess in Zugriff nehmen
Dim Running_Process As New Process()

Running_Process = Process.GetProcessById(Process_Init_ProcessID)

If Running_Process.WaitforExit(30000) = True Then
Process_End_Status = True
Process_Trace = True
Process_End_ExitCode = Running_Process.ExitCode
Else
Process_End_Status = False
Process_Trace = False
Process_End_ExitCode = 0
End If

Running_Process = Nothing

End Function
Nov 20 '05 #3
Hi Christian,

It seems that you have not set the EnableRaisingEvents to true.
There are two ways of being notified when the associated process exits:
synchronously and asynchronously. Synchronous notification relies on
calling the WaitForExit method to pause the processing of your application
until the associated component exits. Asynchronous notification relies on
the Exited event. In either case, EnableRaisingEvents must be set to true
for the Process component to receive notification that the process has
exited.
For more detailed information, please take a look at the link below.
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemdiagnosticsprocessclassexitcodetopic.as p

Here is my test code.
Module Module1
Sub Main()
Dim ps As Process
Dim myProcess As Process
myProcess = Process.Start("NotePad.exe")
ps = Process.GetProcessById(myProcess.Id)
'you may try to comment the code line below to have a test.
ps.EnableRaisingEvents = True
While ps.WaitForExit(3000) = False
Console.WriteLine("running")
'Close the new created notepad, and the program will exit the
loop
End While
Console.WriteLine(ps.ExitCode)
End Sub
End Module

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
Reply-To: "Christian Billig" <ng****@veda.net>
From: "Christian Billig" <ng****@veda.net>
References: <es**************@TK2MSFTNGP09.phx.gbl> <nl**************@cpmsftngxa06.phx.gbl>Subject: Re: Problems with Process Class after WaitforExit
Date: Tue, 11 Nov 2003 10:51:56 +0100
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <u$**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: pns.veda.de 217.6.189.98
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:155826
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi Peter,

here's my code :

Public Function BefehlalsUser(ByVal sCMD As String, ByVal sLogonName As
String, ByVal sDomain As String, ByVal sPassWord As String)
Dim i As Int16
sApplicationName = Trim$(sCMD)

i = InStr(sCMD, ":\")
If i <> 0 Then
sDirectory = Mid$(sCMD, 1, 3)
Else
sDirectory = "C:\"
End If
sb = &H0&

sEnviroment = &H0&

startInfo.cb = Marshal.SizeOf(startInfo)
startInfo.dwFlags = 0&
Process_Init_Status = CreateProcessWithLogon(sLogonName, sDomain,
sPassWord, LOGON_WITH_PROFILE, sApplicationName, sb,
CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP,_

Marshal.StringToBSTR(sEnviroment), sDirectory, startInfo, processInfo)

If (Process_Init_Status = False) Then
Process_Init_ErrorID = Marshal.GetLastWin32Error()
Dim Win32Err As New
System.ComponentModel.Win32Exception(Process_Init _ErrorID)
Process_Init_Error_MSG = Win32Err.Message
Process_Init_Error_HelpLink = Win32Err.HelpLink
Process_Init_Error_Source = Win32Err.Source
Process_Init_ProcessID = 0
Else
Process_Init_ErrorID = 0
Process_Init_Error_MSG = ""
Process_Init_Error_HelpLink = ""
Process_Init_Error_Source = ""
Process_Init_ProcessID = processInfo.dwProcessId
End If

End Function

Public Function Process_Trace() As Boolean
'Prozess verfolgen
'Bei Beendigung muß der ExitCode abgefragt werden
'Erste Schritt Prozess in Zugriff nehmen
Dim Running_Process As New Process()

Running_Process = Process.GetProcessById(Process_Init_ProcessID)

If Running_Process.WaitforExit(30000) = True Then
Process_End_Status = True
Process_Trace = True
Process_End_ExitCode = Running_Process.ExitCode
Else
Process_End_Status = False
Process_Trace = False
Process_End_ExitCode = 0
End If

Running_Process = Nothing

End Function


Nov 20 '05 #4
Hi Peter,

your hint solved the problem.
Everything works fine now thanks a lot.

bye

christian
"Peter Huang" <v-******@online.microsoft.com> schrieb im Newsbeitrag
news:AI***************@cpmsftngxa06.phx.gbl...
Hi Christian,

It seems that you have not set the EnableRaisingEvents to true.
There are two ways of being notified when the associated process exits:
synchronously and asynchronously. Synchronous notification relies on
calling the WaitForExit method to pause the processing of your application
until the associated component exits. Asynchronous notification relies on
the Exited event. In either case, EnableRaisingEvents must be set to true
for the Process component to receive notification that the process has
exited.
For more detailed information, please take a look at the link below.
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemdiagnosticsprocessclassexitcodetopic.as p

Here is my test code.
Module Module1
Sub Main()
Dim ps As Process
Dim myProcess As Process
myProcess = Process.Start("NotePad.exe")
ps = Process.GetProcessById(myProcess.Id)
'you may try to comment the code line below to have a test.
ps.EnableRaisingEvents = True
While ps.WaitForExit(3000) = False
Console.WriteLine("running")
'Close the new created notepad, and the program will exit the
loop
End While
Console.WriteLine(ps.ExitCode)
End Sub
End Module

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
Reply-To: "Christian Billig" <ng****@veda.net>
From: "Christian Billig" <ng****@veda.net>
References: <es**************@TK2MSFTNGP09.phx.gbl>

<nl**************@cpmsftngxa06.phx.gbl>
Subject: Re: Problems with Process Class after WaitforExit
Date: Tue, 11 Nov 2003 10:51:56 +0100
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <u$**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: pns.veda.de 217.6.189.98
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:155826
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi Peter,

here's my code :

Public Function BefehlalsUser(ByVal sCMD As String, ByVal sLogonName AsString, ByVal sDomain As String, ByVal sPassWord As String)
Dim i As Int16
sApplicationName = Trim$(sCMD)

i = InStr(sCMD, ":\")
If i <> 0 Then
sDirectory = Mid$(sCMD, 1, 3)
Else
sDirectory = "C:\"
End If
sb = &H0&

sEnviroment = &H0&

startInfo.cb = Marshal.SizeOf(startInfo)
startInfo.dwFlags = 0&
Process_Init_Status = CreateProcessWithLogon(sLogonName, sDomain,
sPassWord, LOGON_WITH_PROFILE, sApplicationName, sb,
CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or

CREATE_NEW_PROCESS_GROUP,
_

Marshal.StringToBSTR(sEnviroment), sDirectory, startInfo, processInfo)

If (Process_Init_Status = False) Then
Process_Init_ErrorID = Marshal.GetLastWin32Error()
Dim Win32Err As New
System.ComponentModel.Win32Exception(Process_Init _ErrorID)
Process_Init_Error_MSG = Win32Err.Message
Process_Init_Error_HelpLink = Win32Err.HelpLink
Process_Init_Error_Source = Win32Err.Source
Process_Init_ProcessID = 0
Else
Process_Init_ErrorID = 0
Process_Init_Error_MSG = ""
Process_Init_Error_HelpLink = ""
Process_Init_Error_Source = ""
Process_Init_ProcessID = processInfo.dwProcessId
End If

End Function

Public Function Process_Trace() As Boolean
'Prozess verfolgen
'Bei Beendigung muß der ExitCode abgefragt werden
'Erste Schritt Prozess in Zugriff nehmen
Dim Running_Process As New Process()

Running_Process = Process.GetProcessById(Process_Init_ProcessID)

If Running_Process.WaitforExit(30000) = True Then
Process_End_Status = True
Process_Trace = True
Process_End_ExitCode = Running_Process.ExitCode
Else
Process_End_Status = False
Process_Trace = False
Process_End_ExitCode = 0
End If

Running_Process = Nothing

End Function

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Bob | last post: by
9 posts views Thread by Eran.Yasso@gmail.com | last post: by
5 posts views Thread by andrew | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.