1 Replies - 4068 Views - Last Post: 19 July 2009 - 06:51 AM Rate Topic: -----

#1 usminion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-July 09

Problem with System.Diagnostics.Process.GetProcesses

Post icon  Posted 18 July 2009 - 10:36 PM

I am fairly new to VB.NET (but have vast professional experience with several other languages) and am completely new to these forums (so please don't burn me if I posted in the wrong place). I have run into a rather odd quirk with System.Diagnostics.Process.GetProcesses. I have a sample application (source posted below) that checks all running processes every second for changes (created and destroyed) and tells the user what has changed, if anything, in the last second. The sample runs for anywhere between 29-72 seconds before issuing an array out of bounds error. It appears that the Windows system processes, SearchProtocolHost.exe and SearchFilterHost.exe explicitly, disappear from the list causing my application to throw the aforementioned exception. Specifically the exception I am receiving is as follows:

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

I have noted in comments where the exception is thrown in the code below. If any of you would be willing to try this sample on your PC's or could suggest potential issues with my sample that could cause this problem I would be very appreciative.

My sample code:
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Management
Imports Microsoft.Win32
Imports System.Collections
Imports System.Collections.Generic
Imports System.Threading

Namespace ProcessMonitor
	Class ProcessMonitor
		Shared bFinished As Boolean = False
		Dim dictRunning As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

		Shared Sub GetInput()
			While Not bFinished
				Console.ReadKey(True)
				bFinished = True
			End While
		End Sub

		Sub BindToRunningProcesses()
			Dim procInfo As Process
			Dim currProc As KeyValuePair(Of String, Integer)
			Dim listRemove As List(Of String) = New List(Of String)
			Dim listReset As List(Of String) = New List(Of String)
			Dim arrProc As Process()
			Dim times As Integer

			' Loop until key pressed
			While Not bFinished
				arrProc = Process.GetProcesses()

				' Loop through all of the known processes
				For Each procInfo In arrProc
					Try
						If Not Me.dictRunning.ContainsKey(procInfo.MainModule.ModuleName) Then
							Console.WriteLine("Started: " & procInfo.MainModule.ModuleName)
							Me.dictRunning.Add(procInfo.MainModule.ModuleName, 1)
						Else
							Me.dictRunning(procInfo.MainModule.ModuleName) = 1
						End If
					Catch ex As Exception
						' Ignore Exceptions
						'Console.WriteLine("Exception (" & procInfo.ProcessName + "): " & ex.Message)
					End Try
				Next

				Dim count As Integer = Me.dictRunning.Count
				For i As Integer = 0 To (count - 1)
					' The following line is where the exception is thrown
					currProc = Me.dictRunning.ElementAt(i)
					If currProc.Value = 0 Then
						Console.WriteLine("Stopped: " & currProc.Key)
						Me.dictRunning.Remove(currProc.Key)
						count = count - 1
					Else
						Me.dictRunning(currProc.Key) = 0
					End If
				Next

				times = times + 1
				Console.WriteLine(times)
				Thread.Sleep(1000)
			End While
		End Sub 'BindToRunningProcesses

		Shared Sub Main()
			Dim myProcess As New ProcessMonitor()
			Dim T As Thread = New Thread(AddressOf GetInput)

			T.Start()
			myProcess.BindToRunningProcesses()
		End Sub 'Main 
	End Class
End Namespace


I am running VB.NET 2008 Express Edition (Framework v3.5) on Vista Home Premium with SP1

This post has been edited by usminion: 18 July 2009 - 10:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with System.Diagnostics.Process.GetProcesses

#2 usminion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-July 09

Re: Problem with System.Diagnostics.Process.GetProcesses

Posted 19 July 2009 - 06:51 AM

I found the issue... thanks!

View Postusminion, on 18 Jul, 2009 - 09:36 PM, said:

I am fairly new to VB.NET (but have vast professional experience with several other languages) and am completely new to these forums (so please don't burn me if I posted in the wrong place). I have run into a rather odd quirk with System.Diagnostics.Process.GetProcesses. I have a sample application (source posted below) that checks all running processes every second for changes (created and destroyed) and tells the user what has changed, if anything, in the last second. The sample runs for anywhere between 29-72 seconds before issuing an array out of bounds error. It appears that the Windows system processes, SearchProtocolHost.exe and SearchFilterHost.exe explicitly, disappear from the list causing my application to throw the aforementioned exception. Specifically the exception I am receiving is as follows:

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

I have noted in comments where the exception is thrown in the code below. If any of you would be willing to try this sample on your PC's or could suggest potential issues with my sample that could cause this problem I would be very appreciative.

My sample code:
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Management
Imports Microsoft.Win32
Imports System.Collections
Imports System.Collections.Generic
Imports System.Threading

Namespace ProcessMonitor
	Class ProcessMonitor
		Shared bFinished As Boolean = False
		Dim dictRunning As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

		Shared Sub GetInput()
			While Not bFinished
				Console.ReadKey(True)
				bFinished = True
			End While
		End Sub

		Sub BindToRunningProcesses()
			Dim procInfo As Process
			Dim currProc As KeyValuePair(Of String, Integer)
			Dim listRemove As List(Of String) = New List(Of String)
			Dim listReset As List(Of String) = New List(Of String)
			Dim arrProc As Process()
			Dim times As Integer

			' Loop until key pressed
			While Not bFinished
				arrProc = Process.GetProcesses()

				' Loop through all of the known processes
				For Each procInfo In arrProc
					Try
						If Not Me.dictRunning.ContainsKey(procInfo.MainModule.ModuleName) Then
							Console.WriteLine("Started: " & procInfo.MainModule.ModuleName)
							Me.dictRunning.Add(procInfo.MainModule.ModuleName, 1)
						Else
							Me.dictRunning(procInfo.MainModule.ModuleName) = 1
						End If
					Catch ex As Exception
						' Ignore Exceptions
						'Console.WriteLine("Exception (" & procInfo.ProcessName + "): " & ex.Message)
					End Try
				Next

				Dim count As Integer = Me.dictRunning.Count
				For i As Integer = 0 To (count - 1)
					' The following line is where the exception is thrown
					currProc = Me.dictRunning.ElementAt(i)
					If currProc.Value = 0 Then
						Console.WriteLine("Stopped: " & currProc.Key)
						Me.dictRunning.Remove(currProc.Key)
						count = count - 1
					Else
						Me.dictRunning(currProc.Key) = 0
					End If
				Next

				times = times + 1
				Console.WriteLine(times)
				Thread.Sleep(1000)
			End While
		End Sub 'BindToRunningProcesses

		Shared Sub Main()
			Dim myProcess As New ProcessMonitor()
			Dim T As Thread = New Thread(AddressOf GetInput)

			T.Start()
			myProcess.BindToRunningProcesses()
		End Sub 'Main 
	End Class
End Namespace


I am running VB.NET 2008 Express Edition (Framework v3.5) on Vista Home Premium with SP1

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1