Hi,
I am trying to create an array of WaitHandles that contain process handles returned from Process.GetProcesses. The code compiles but at runtime I get a System.NullReferenceException - "Object reference not set to an instance of an object." at the statement whandles[i].SafeWaitHandle = swh;
The code is as follows:
What's wrong?Code:using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Threading; using Microsoft.Win32.SafeHandles; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Process[] processlist = Process.GetProcesses(); List<Process> closeProcesses = new List<Process>(); foreach (Process p in processlist) { if (p.ProcessName == "FApp1" || p.ProcessName == "Fapp2" || p.ProcessName == "FApp3") { closeProcesses.Add(p); } } int processesOpen = closeProcesses.Count; WaitHandle[] whandles = new WaitHandle[processesOpen]; for (int i = 0; i < processlist.Length; ++i) { SafeWaitHandle swh = new SafeWaitHandle(closeProcesses[i].Handle, false); whandles[i].SafeWaitHandle = swh; } WaitHandle.WaitAll(whandles); System.Console.WriteLine("All closed!"); } } }




Reply With Quote