I have 2 queries.
I have created working with cpu monitoring, I have retrieved value for CPU Usage, Ram Usage(memory), were as CPU usage has been calculated in percentage.
1. I need Memory Usage as Percentage value. I have tried but i couldn't reach.
2. When i host this application in Windows Service, How i can identify which machine usage consumption.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static List<float> AvailableCPU = new List<float>();
static List<float> AvailableRAM = new List<float>();
protected static PerformanceCounter cpuCounter;
protected static PerformanceCounter ramCounter;
private void Form1_Load(object sender, EventArgs e)
{
cpumonitor("CL35");
}
public void cpumonitor(string machinename)
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
cpuCounter.MachineName = machinename;
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
try
{
System.Timers.Timer t = new System.Timers.Timer(5000);
t.Elapsed += new ElapsedEventHandler(TimerElapsed);
t.Start();
Thread.Sleep(10000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "catched exception");
}
}
public static void TimerElapsed(object source, ElapsedEventArgs e)
{
float cpu = cpuCounter.NextValue();
float ram = ramCounter.NextValue();
MessageBox.Show(string.Format("CPU Value: {0}, ram value: {1}", cpu, ram));
AvailableCPU.Add(cpu);
AvailableRAM.Add(ram);
}
}
What I have tried:
I have tried for getting total memory usage.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static List<float> AvailableCPU = new List<float>();
static List<float> AvailableRAM = new List<float>();
protected static PerformanceCounter cpuCounter;
protected static PerformanceCounter ramCounter;
protected static PerformanceCounter ramTotMB;
private void Form1_Load(object sender, EventArgs e)
{
cpumonitor("CL35");
}
public void cpumonitor(string machinename)
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
cpuCounter.MachineName = machinename;
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
ramTotMB = new PerformanceCounter("Memory", "Total MBytes");
try
{
System.Timers.Timer t = new System.Timers.Timer(5000);
t.Elapsed += new ElapsedEventHandler(TimerElapsed);
t.Start();
Thread.Sleep(10000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "catched exception");
}
}
public static void TimerElapsed(object source, ElapsedEventArgs e)
{
float cpu = cpuCounter.NextValue();
float ram = ramCounter.NextValue();
float Totram = ramTotMB.NextValue();
MessageBox.Show(string.Format("CPU Value: {0}, ram value: {1},Totalvalue: {2}", cpu, ram,Totram));
AvailableCPU.Add(cpu);
AvailableRAM.Add(ram);
}
}