CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Array of WaitHandles from process handles

  1. #1
    Join Date
    Nov 2006
    Posts
    120

    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?

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  3. #3
    Join Date
    Nov 2006
    Posts
    120

    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.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  5. #5
    Join Date
    Aug 2010
    Posts
    1

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)