473,240 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

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
4 3966
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephen Haeney via .NET 247 | last post by:
I am developing a WinForm application which will redirect theuser to HTML page, using IE, when they click a button. However,I want this to be modal in operation, so I would like to makesure that the...
3
by: James Li | last post by:
I have the code below, the first process takes about 10 minutes to finish. But the waitForExit doesn't seem to wait when I debug the program, it go immediately to execute code after the...
2
by: Terry Olsen | last post by:
Using the following code, I get the error "No process is associated with this object" when calling the WinZip.WaitForExit() method. After I click the "Break" or "Continue" button on the dialog,...
11
by: Nurit N | last post by:
This is the third newsgroup that I'm posting my problem. I'm sorry for the multiple posts but the matter becoming urgent. I hope this is the right place for it... I have created a very...
3
by: Jay | last post by:
hi i am jay from bangalore i have one problem ,can u please help me out. i have one windows application and one windows service. from windows application i have to call one process "gpg" to...
7
by: Bob | last post by:
Process.start("Mydoc.doc") starts Word with the file. I need to wait for Word to be closed before more code can execute in my app. How can I do this? Thanks for any help Bob
9
by: Eran.Yasso | last post by:
Hi, My app starts process. Some times this process exits because of exception. Can my app know if the process exited due to exception or gracefully? In both ways, the exit code of this...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
4
by: Steven De Smet | last post by:
Hello, This is my first post. I searched on the internet for answers but I was unable to solve my problem. So I hope that you guy's can help me with my VB.NET problem I tried to create a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.