Process ExitCode error after HasExited true

D

Dan McGuffin

I am trying to develop some vb.net code that will will store 1 or more processes in an array of processes, then loop through the array determining when each one has exited. Upon each one exiting, I want to display the corresponding process' exitcode. I had the following code working a week ago, but then I made some (mostly) cosmetic changes, and now I can't get it to work.

Imports System
Imports System.Collections
Imports System.Diagnostics
Imports System.Threading

Namespace Process_Sample
Class MyProcessClass
Public Shared Sub Main()
Dim myProcess As Process

myProcess = Process.Start("notepad")

Dim arrayProcess As Process() = Process.GetProcessesByName("notepad")

If arrayProcess.GetUpperBound(0) >= 0 Then
Dim alRunningProcessIdList As New ArrayList
For i As Int16 = 0 To arrayProcess.GetUpperBound(0)
Console.WriteLine("Process id {0} started at {1}", arrayProcess(i).Id, arrayProcess(i).StartTime)
alRunningProcessIdList.Add(arrayProcess(i).Id)
Next

Console.WriteLine("processes launched = {0}", (arrayProcess.GetUpperBound(0) + 1))

Do Until alRunningProcessIdList.Count = 0
Thread.Sleep(1000)
For i As Int16 = 0 To arrayProcess.GetUpperBound(0)
If alRunningProcessIdList.Contains(arrayProcess(i).Id) Then
If arrayProcess(i).HasExited Then
Console.WriteLine("Process Id {0} has exited", arrayProcess(i).Id)
Console.WriteLine("with an exit code of {0}", arrayProcess(i).ExitCode)
alRunningProcessIdList.Remove(arrayProcess(i).Id)
End If
End If
Next
Loop
Else
Console.WriteLine("No processes launched!")
End If
Console.WriteLine("Press <Enter> to exit...")
Console.ReadLine()
End Sub
End Class
End Namespace

The output I get in the console window is:

Process id 1068 started at 1/28/2004 4:29:09 PM
processes launched = 1
Process Id 1068 has exited

The program throws an exception on the line highlighted in red above. The error message is:

"An unhandled exception of type 'System.InvalidOperationException' occurred in system.dll

Additional information: Process was not started by this object, so requested information cannot be determined."

If anyone can help me, I would greatly appreciate it. Please reply to this post, or to (e-mail address removed).

Thanks,
Dan McGuffin
(e-mail address removed)
 

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