473,564 Members | 2,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
CreateProcessWi thLogon.

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 3991
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.ne t>
From: "Christian Billig" <ng****@veda.ne t>
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.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: pns.veda.de 217.6.189.98
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP09.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:155505
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
CreateProcessW ithLogon.

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(B yVal sCMD As String, ByVal sLogonName As
String, ByVal sDomain As String, ByVal sPassWord As String)
Dim i As Int16
sApplicationNam e = 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.dwFla gs = 0&
Process_Init_St atus = CreateProcessWi thLogon(sLogonN ame, sDomain,
sPassWord, LOGON_WITH_PROF ILE, sApplicationNam e, sb,
CREATE_DEFAULT_ ERROR_MODE Or CREATE_NEW_CONS OLE Or CREATE_NEW_PROC ESS_GROUP,
_

Marshal.StringT oBSTR(sEnvirome nt), sDirectory, startInfo, processInfo)

If (Process_Init_S tatus = False) Then
Process_Init_Er rorID = Marshal.GetLast Win32Error()
Dim Win32Err As New
System.Componen tModel.Win32Exc eption(Process_ Init_ErrorID)
Process_Init_Er ror_MSG = Win32Err.Messag e
Process_Init_Er ror_HelpLink = Win32Err.HelpLi nk
Process_Init_Er ror_Source = Win32Err.Source
Process_Init_Pr ocessID = 0
Else
Process_Init_Er rorID = 0
Process_Init_Er ror_MSG = ""
Process_Init_Er ror_HelpLink = ""
Process_Init_Er ror_Source = ""
Process_Init_Pr ocessID = processInfo.dwP rocessId
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.GetProc essById(Process _Init_ProcessID )

If Running_Process .WaitforExit(30 000) = True Then
Process_End_Sta tus = True
Process_Trace = True
Process_End_Exi tCode = Running_Process .ExitCode
Else
Process_End_Sta tus = False
Process_Trace = False
Process_End_Exi tCode = 0
End If

Running_Process = Nothing

End Function
Nov 20 '05 #3
Hi Christian,

It seems that you have not set the EnableRaisingEv ents 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, EnableRaisingEv ents 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/
frlrfsystemdiag nosticsprocessc lassexitcodetop ic.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.GetProc essById(myProce ss.Id)
'you may try to comment the code line below to have a test.
ps.EnableRaisin gEvents = True
While ps.WaitForExit( 3000) = False
Console.WriteLi ne("running")
'Close the new created notepad, and the program will exit the
loop
End While
Console.WriteLi ne(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.ne t>
From: "Christian Billig" <ng****@veda.ne t>
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.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: pns.veda.de 217.6.189.98
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:155826
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb

Hi Peter,

here's my code :

Public Function BefehlalsUser(B yVal sCMD As String, ByVal sLogonName As
String, ByVal sDomain As String, ByVal sPassWord As String)
Dim i As Int16
sApplicationNam e = 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.dwFla gs = 0&
Process_Init_St atus = CreateProcessWi thLogon(sLogonN ame, sDomain,
sPassWord, LOGON_WITH_PROF ILE, sApplicationNam e, sb,
CREATE_DEFAULT _ERROR_MODE Or CREATE_NEW_CONS OLE Or CREATE_NEW_PROC ESS_GROUP,_

Marshal.String ToBSTR(sEnvirom ent), sDirectory, startInfo, processInfo)

If (Process_Init_S tatus = False) Then
Process_Init_Er rorID = Marshal.GetLast Win32Error()
Dim Win32Err As New
System.Compone ntModel.Win32Ex ception(Process _Init_ErrorID)
Process_Init_Er ror_MSG = Win32Err.Messag e
Process_Init_Er ror_HelpLink = Win32Err.HelpLi nk
Process_Init_Er ror_Source = Win32Err.Source
Process_Init_Pr ocessID = 0
Else
Process_Init_Er rorID = 0
Process_Init_Er ror_MSG = ""
Process_Init_Er ror_HelpLink = ""
Process_Init_Er ror_Source = ""
Process_Init_Pr ocessID = processInfo.dwP rocessId
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.GetProc essById(Process _Init_ProcessID )

If Running_Process .WaitforExit(30 000) = True Then
Process_End_Sta tus = True
Process_Trace = True
Process_End_Exi tCode = Running_Process .ExitCode
Else
Process_End_Sta tus = False
Process_Trace = False
Process_End_Exi tCode = 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.m icrosoft.com> schrieb im Newsbeitrag
news:AI******** *******@cpmsftn gxa06.phx.gbl.. .
Hi Christian,

It seems that you have not set the EnableRaisingEv ents 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, EnableRaisingEv ents 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/ frlrfsystemdiag nosticsprocessc lassexitcodetop ic.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.GetProc essById(myProce ss.Id)
'you may try to comment the code line below to have a test.
ps.EnableRaisin gEvents = True
While ps.WaitForExit( 3000) = False
Console.WriteLi ne("running")
'Close the new created notepad, and the program will exit the
loop
End While
Console.WriteLi ne(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.ne t>
From: "Christian Billig" <ng****@veda.ne t>
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.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: pns.veda.de 217.6.189.98
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:155826
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb

Hi Peter,

here's my code :

Public Function BefehlalsUser(B yVal sCMD As String, ByVal sLogonName AsString, ByVal sDomain As String, ByVal sPassWord As String)
Dim i As Int16
sApplicationNam e = 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.dwFla gs = 0&
Process_Init_St atus = CreateProcessWi thLogon(sLogonN ame, sDomain,
sPassWord, LOGON_WITH_PROF ILE, sApplicationNam e, sb,
CREATE_DEFAULT _ERROR_MODE Or CREATE_NEW_CONS OLE Or

CREATE_NEW_PROC ESS_GROUP,
_

Marshal.String ToBSTR(sEnvirom ent), sDirectory, startInfo, processInfo)

If (Process_Init_S tatus = False) Then
Process_Init_Er rorID = Marshal.GetLast Win32Error()
Dim Win32Err As New
System.Compone ntModel.Win32Ex ception(Process _Init_ErrorID)
Process_Init_Er ror_MSG = Win32Err.Messag e
Process_Init_Er ror_HelpLink = Win32Err.HelpLi nk
Process_Init_Er ror_Source = Win32Err.Source
Process_Init_Pr ocessID = 0
Else
Process_Init_Er rorID = 0
Process_Init_Er ror_MSG = ""
Process_Init_Er ror_HelpLink = ""
Process_Init_Er ror_Source = ""
Process_Init_Pr ocessID = processInfo.dwP rocessId
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.GetProc essById(Process _Init_ProcessID )

If Running_Process .WaitforExit(30 000) = True Then
Process_End_Sta tus = True
Process_Trace = True
Process_End_Exi tCode = Running_Process .ExitCode
Else
Process_End_Sta tus = False
Process_Trace = False
Process_End_Exi tCode = 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
325
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 IE process has finished before I allow then tocontinue. I first tried using the WaitForExit method of the Process class,but this returns an error...
3
22488
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 WaitForExit(). The first process basically launch a DOS batch file to do some task, am I missing anything? executable = "setup_listener.bat"; process1 =...
2
18344
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, the app exits but then the command window opens and the process runs on its own. Could the "WaitForExit" call be happening too soon? Private Sub...
11
3727
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 simple batch file (echo hello world) and was trying to retrieve the standard output but every time I run the code it returns ExitCode as 1.
3
3617
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 encrypt my text file so i use system.Diagnostics.process class to make new process. my code is like this
7
7309
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
18863
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 process is zero. I tried using the following, but it goes to catch.
5
5067
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 = proc.Start("notepad.exe");
4
8810
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 windows service that converts MS Word Files into .PDF files and after that we want to zip the .PDF files. Our code: Protected Overrides Sub...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.