Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get process information

Posted on 2008-06-23
11
Medium Priority
?
3,115 Views
Last Modified: 2013-11-05
I started to write a class that will get certain information regarding processes.  The code below is as far as I got.  I'm hoping for code/help to polish this off:

1)  IsProcessAlive(hostname, processname)             return bool
2)  GetProcessCPU(hostname, processname)           return text or numeric (% use of CPU)
3)  GetProcessRAM(hostname, processname)           return text or numeric (% use of RAM)

class CheckProcess : Action
    {
        public CheckProcess(string name)
            : base(name)
        {
            type_ = "CheckProcess";
        }

        public override string Execute()   //******* is this line correct given the fact I need to pass parameters (hostname, processname)
        {
            Process[] processlist = Process.GetProcesses();

            foreach(Process theprocess in processlist){
                Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
            }
            //  haven't made use of this stuff below yet
            //p.StartTime (Shows the time the process started)
            //p.TotalProcessorTime (Shows the amount of CPU time the process has taken)
            //p.Threads ( gives access to the collection of threads in the process)

        }
    }

Thanks!
0
Comment
Question by:John500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 13

Accepted Solution

by:
iHadi earned 2000 total points
ID: 21851127
Hi John500

Try the attached code
PerformanceCounter pc = new PerformanceCounter();
 
public Process GetProcess(string hostname, string processName)
{
    Process[] ps = Process.GetProcessesByName(processName, hostname);
 
    if (ps.Length == 0)
        return null;
    else
        return ps[0];
}
 
public bool IsProcessAlive(string hostname, string processName)
{
    Process p = GetProcess(hostname, processName);
 
    if (p == null)
    {
        return false;
    }
    else
    {
        return !p.HasExited;
    }
}
 
public string GetProcessCPU(string hostname, string processName)
{
    string str = "";
 
    pc.CategoryName = "Process";
    pc.CounterName = "% Processor Time";
    pc.InstanceName = processName;
    pc.MachineName = hostname;
 
    try
    {
        str = pc.NextValue().ToString();
    }
    catch
    {
        str = "Error";
    }
 
    return str;
}
 
public string GetProcessRAM(string hostname, string processName)
{
    Process p = GetProcess(hostname, processName);
 
    if (p == null)
    {
        return "Error";
    }
    else
    {
        return p.PagedMemorySize64.ToString();
    }
}

Open in new window

0
 

Author Comment

by:John500
ID: 21851320
iHadi,

Thanks for the help.

I have a couple of problems that I'm sure are straight forward.  I simplified the project and made one class and one file for the project.  Can you tell me how I would correct the errors below?  

Thanks

Error      1      'ProcessInfo.ProcessInfo' does not contain a definition for 'CategoryName'      C:\C # apps\ProcessApp\GetProcess.cs      40      16      ProcessApp
Error      2      'ProcessInfo.ProcessInfo' does not contain a definition for 'CounterName'      C:n\C # apps\ProcessApp\GetProcess.cs      41      16      ProcessApp
Error      3      'ProcessInfo.ProcessInfo' does not contain a definition for 'InstanceName'      C:\C # apps\ProcessApp\GetProcess.cs      42      16      ProcessApp
Error      4      'ProcessInfo.ProcessInfo' does not contain a definition for 'MachineName'      C:n\C # apps\ProcessApp\GetProcess.cs      43      16      ProcessApp
Error      5      'ProcessInfo.ProcessInfo' does not contain a definition for 'NextValue'      C:\C # apps\ProcessApp\GetProcess.cs      47      26      ProcessApp

using System;
using System.IO;
using System.Diagnostics;
 
namespace ProcessInfo
{
    class ProcessInfo
    {
        ProcessInfo pc = new ProcessInfo();
 
        public Process GetProcess(string hostname, string processName)
        {
            Process[] ps = Process.GetProcessesByName(processName, hostname);
 
            if (ps.Length == 0)
                return null;
            else
                return ps[0];
        }
 
        public bool IsProcessAlive(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return false;
            }
            else
            {
                return !p.HasExited;
            }
        }
 
        public string GetProcessCPU(string hostname, string processName)
        {
            string str = "";
 
            pc.CategoryName = "Process";
            pc.CounterName = "% Processor Time";
            pc.InstanceName = processName;
            pc.MachineName = hostname;
 
            try
            {
                str = pc.NextValue().ToString();
            }
            catch
            {
                str = "Error";
            }
 
            return str;
        }
 
        public string GetProcessRAM(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return "Error";
            }
            else
            {
                return p.PagedMemorySize64.ToString();
            }
        }
    }
}

Open in new window

0
 
LVL 13

Expert Comment

by:iHadi
ID: 21851367
Replace the line:
         ProcessInfo pc = new ProcessInfo();

With:
         PerformanceCounter pc = new PerformanceCounter();
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:John500
ID: 21851423
I know I needed to rename the class to something like 'CheckProcessInfo' and the class to something like 'CheckProcess'

But what about those lousy references?  What do I need and in what order??

Thanks!
0
 
LVL 13

Expert Comment

by:iHadi
ID: 21851483
My previous post is not about renaming your class.

You have declared pc as type 'ProcessInfo' where it should be of type 'PerformanceCounter', so you need to replace:

ProcessInfo pc = new ProcessInfo();

With:

PerformanceCounter pc = new PerformanceCounter();

You already have the references that you need: using System.Diagnostics;
0
 
LVL 13

Expert Comment

by:iHadi
ID: 21851489
Your code should look like this:
using System;
using System.IO;
using System.Diagnostics;
 
namespace ProcessInfoExtractor
{
    class ProcessInfo
    {
        PerformanceCounter pc = new PerformanceCounter();
 
        public Process GetProcess(string hostname, string processName)
        {
            Process[] ps = Process.GetProcessesByName(processName, hostname);
 
            if (ps.Length == 0)
                return null;
            else
                return ps[0];
        }
 
        public bool IsProcessAlive(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return false;
            }
            else
            {
                return !p.HasExited;
            }
        }
 
        public string GetProcessCPU(string hostname, string processName)
        {
            string str = "";
 
            pc.CategoryName = "Process";
            pc.CounterName = "% Processor Time";
            pc.InstanceName = processName;
            pc.MachineName = hostname;
 
            try
            {
                str = pc.NextValue().ToString();
            }
            catch
            {
                str = "Error";
            }
 
            return str;
        }
 
        public string GetProcessRAM(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return "Error";
            }
            else
            {
                return p.PagedMemorySize64.ToString();
            }
        }
    }
}

Open in new window

0
 

Author Comment

by:John500
ID: 21858025
Still having a problem with those references.  Using the code exactly as it appears above in your last post - I get these error:

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;

namespace ProcessInfoExtractor
{
    class ProcessInfo
    {
        PerformanceCounter pc = new PerformanceCounter();      //  ******** line 10

        public Process GetProcess(string hostname, string processName)     //  ******** line 10
        {
            Process[] ps = Process.GetProcessesByName(processName, hostname);    
...
...

Error      1      The type or namespace name 'PerformanceCounter' could not be found (are you missing a using directive or an assembly reference?)      C:\@john\test\C#\GetProcessInfo\ProcessInfo.cs      10      9      GetProcessInfo
Error      2      The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)      C:\@john\test\C#\GetProcessInfo\ProcessInfo.cs      12      16      GetProcessInfo
0
 

Author Comment

by:John500
ID: 21858047
I forgot to change that second '10' to '12' ......

Here is a picture of my references:
references.JPG
0
 

Author Comment

by:John500
ID: 21860084
I don't know how but it straightened itself out.  The program compiles and works.

Can you tell me how I can accurately display the following value for RAM:

24166400

I close this out in the mean time.

Thanks
0
 

Author Comment

by:John500
ID: 21860194
Also,

For the line that reads:

public bool IsProcessAlive(string hostname, string processName)
{
...
           return !p.HasExited;                    // error:  Feature is not supported for remote machines.  (NotSupportedException was unhandled)
...
}

The parameter was:

 \\MachineName
0
 
LVL 13

Expert Comment

by:iHadi
ID: 21865728
The RAM value is in bytes. divide it by 1024 to get it in KB and another 1024 to get it in MB

As for the IsProcessAlive, it's ok to replace the line

return !p.HasExited;

with

return true;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question