-
December 1st, 2008, 04:01 PM
#1
Array of WaitHandles from process handles
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:
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!");
}
}
}
What's wrong?
-
December 1st, 2008, 06:33 PM
#2
Re: Array of WaitHandles from process handles
Code:
for (int i = 0; i < processlist.Length; ++i)
What you probably meant was:
Code:
for (int i = 0; i < closeProcesses.Length; ++i)
Or better yet, use a for each statement.
- petter
-
December 1st, 2008, 08:10 PM
#3
Re: Array of WaitHandles from process handles
Thanks for your reply. Yes, that was definitely a mistake, but it doesn't solve the problem. Exactly the same runtime error occurs.
-
December 1st, 2008, 09:59 PM
#4
Re: Array of WaitHandles from process handles
Ahh. Another snag. Your array of WaitHandles is initialized, but the individual elements are not. So when you try to access the whandles[i].SafeWaitHandle property you'll get an NullReferenceException.
To solve this you'll have to instantiate the WaitHandle. Only problem is that WaitHandle has protected access to its constructor. A workaround is to create an instance of a ManualResetEvent or similar (don't knnow of a better approach):
Code:
SafeWaitHandle swh = new SafeWaitHandle(closeProcesses[i].Handle, false);
whandles[i] = new AutoResetEvent(false); // first create a 'normal' wait handle
whandles[i].SafeWaitHandle = swh; // then assign our safe wait handle. This should release the original handle.
You should also make sure that the final array holds atleast 1 element, or your call to WaitAll(...) will fail.
- petter
-
August 23rd, 2010, 12:43 PM
#5
Re: Array of WaitHandles from process handles
Thanks for this - I was needing to detect exit of unmanaged base process from c++/cli shim
This is what i came up with, your last post was the hint i needed.
//Get a wait mechanism for the base process
m_eventArray[0] = new ManualResetEvent(false);
m_eventArray[0].SafeWaitHandle = new SafeWaitHandle(System.Diagnostics.Process.GetCurrentProcess().Handle, false);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|