Resources for IT Professionals > 포럼 홈 > .NET Base Class Library > I get an exception in Process.getProcesses()

답변됨 I get an exception in Process.getProcesses()

  • 2007년 6월 7일 목요일 오전 11:36
     
     
    I am working on a utility which has to report for the termination of a process. I am using Process.getProcesses() to get all the processes and use .HasExited property to check if it has terminated and then report it. But i get an exception thrown in the below line..

    Process[] proc = Process.GetProcesses(".");

    the exception i get is as follows

    Couldn't get process information from performance counter.
    System.ComponentModel.Win32Exception: Unknown error (0xc000009a)
       at System.Diagnostics.NtProcessInfoHelper.GetProcessInfos()
       at System.Diagnostics.ProcessManager.GetProcessInfos(String machineName)
       at System.Diagnostics.Process.GetProcesses(String machineName)
       at MonitorUtility.Program.Main(String[] args) in

    I am working on Windows Xp professional and .netframewok v2.0

    Can anyone help me out from this or any other method to track the termination of processes

답변

  • 2007년 6월 18일 월요일 오전 7:01
     
     답변됨

    Going back to your original question, are you running the code that calls Process.GetProcesses from Visual Studio (i.e. you just press F5 or whatever to start it up)?

     

    I ask because when you run from Visual Studio, your code always runs inside the same instance of app.vshost.exe. This is important when using unmanaged resources like those returned from Process.GetProcesses - you must ensure you clean them up properly when your program exits by calling Process.Close on each one. Normally, when your process exist, the OS does this for you, but inside app.vshost.exe, the process doesn't actually exist and so you'll end up leaking handles.

     

    As others have suggested, you can use Process Explorer (though even good old Task Manager will work) to check the number of handles allocated to each process. Take special note of your app.vshost.exe process...

모든 응답

  • 2007년 6월 7일 목요일 오후 12:05
    중재자
     
     
    That error code means "Insufficient resources".  Reboot your PC.  Use a tool like Process Explorer to make sure you are not leaking kernel handles.  Try calling Dispose() on the process instances before refreshing the array with GetProcesses().
  • 2007년 6월 7일 목요일 오후 12:32
     
     
    But i call the function only ones in the beginning and use
    ProcIdea.HasExited to check whether it has terminated in a while loop.
    My system is running crictical applications and it can't shutdown as so easily. can eloborate on what is leaking of kernel handles????
  • 2007년 6월 7일 목요일 오후 12:50
    중재자
     
     
    If the machine you are trying this on has been running for a while, you definitely should consider a reboot and try again.  Perhaps it is executing long-running programs that do not properly release resources after using them (like forgetting to close a file or leaking memory).  Process Explorer can help you find that program.  The number of handles used by a program is shown in the Performance tab of the Properties dialog.  Anything over 5000 handles deserves a good look.  Private bytes tells you the amount of memory it uses.  It depends, but 100 MB or more is a warning sign.
  • 2007년 6월 8일 금요일 오전 4:41
     
     
    I tried another method using WMI in that System.management namespace to monitor processes.. I subscribe to a process deletion event.. At times it happens that i get the event take place for all the processes that are running in my system. Is it again because of this system resources or it could be someother reason..
  • 2007년 6월 12일 화요일 오전 6:25
    중재자
     
     

    Hi Tom Praison,

       I'm not very sure about what you mean by"Is it again because of this system resources or it could be someother reason..".

       Do you mean that you've resolved the problem using WMI event handler?

    Thanks!

  • 2007년 6월 13일 수요일 오전 5:08
     
     
    Since the Process.getProcesses() throws exception i thought to use the WMI to monitor the processes. But in WMI i subscribe to a deletion event. here too i have come across a problem. The problem is that sometimes the WMI calls a deletion event for all the processes in my system even when they are all still running. I want to know whether this is because of the lack of system resources or some other reason. It happens only sometimes, when there are more applications running. I need to monitor the processes running on a server and send an email if one of my critical processes goes down. But it is sending mails even if the processes are still running
  • 2007년 6월 13일 수요일 오전 5:48
    중재자
     
     

    Hi Tom Praison,
      To troubleshoot this issue, we really need the source code or a sample project to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.

    Thanks!

  • 2007년 6월 13일 수요일 오전 8:38
     
     
    Hi here is the part of the source code hope it helps you. Thanks in advance

    try
                {
                    string scope = @"\\.\root\CIMV2";
                  
                    ManagementScope MgtScope = new ManagementScope(scope);
                    // create the wql query to subscribe to a process deletion event 
                    WqlEventQuery query =
                        new WqlEventQuery("__InstanceDeletionEvent",
                        new TimeSpan(0, 0, 10),
                        "TargetInstance isa \"Win32_Process\"");

                    // Initialize an event watcher and subscribe to events
                    // that match this query
                    //for deletion event
                    ManagementEventWatcher watcherDel =
                        new ManagementEventWatcher() ;
                    watcherDel.Query = query;
                    watcherDel.Scope = MgtScope;
                    watcherDel.EventArrived += new EventArrivedEventHandler(DeletionArrived);
                    watcherDel.Start();
                  
                    Console.ReadLine();
                    watcherDel.Stop();
                    watcherDel.Dispose();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                    Console.ReadLine();
                }

    private void DeletionArrived(object sender, EventArrivedEventArgs e)
            {
                ManagementBaseObject eventArg = (ManagementBaseObject)(e.NewEvent["TargetInstance"]);                    
                    string ProcessName = eventArg["Name"].ToString();
    Console.WriteLine(ProcessName);       
    //check for my processes and send mail

    }

    Note: This happens mostly when there are lots of applications are running.. Example: when I have 2 or more Visual Studio IDE opened etc..
  • 2007년 6월 16일 토요일 오전 6:22
     
     

     

    Do you have any special case or any observation with specific process or it is usually happened when you have lots of process running ?

  • 2007년 6월 18일 월요일 오전 2:24
     
     
    Mostly it happens when i have the visual studio IDE open with 2 or more projects but i am not sure whether it happens because of this
  • 2007년 6월 18일 월요일 오전 7:01
     
     답변됨

    Going back to your original question, are you running the code that calls Process.GetProcesses from Visual Studio (i.e. you just press F5 or whatever to start it up)?

     

    I ask because when you run from Visual Studio, your code always runs inside the same instance of app.vshost.exe. This is important when using unmanaged resources like those returned from Process.GetProcesses - you must ensure you clean them up properly when your program exits by calling Process.Close on each one. Normally, when your process exist, the OS does this for you, but inside app.vshost.exe, the process doesn't actually exist and so you'll end up leaking handles.

     

    As others have suggested, you can use Process Explorer (though even good old Task Manager will work) to check the number of handles allocated to each process. Take special note of your app.vshost.exe process...