Problems with Process Class after WaitforExit

C

Christian Billig

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
 
P

Peter Huang

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

Christian Billig

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
 
P

Peter Huang

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/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdiagnosticsprocessclassexitcodetopic.asp

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

Christian Billig

Hi Peter,

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

bye

christian
 

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