Friday 31 July 2015

Get RAM and CPU usage in C#

Get Cpu and Ram Usage by kernel132.dll in c# How to get CPU percentage ?
  1. Step 1: include namespace in class file
     using System;
     using System.Diagnostics;
     using System.Runtime.InteropServices;
     using System.IO;
    
  2. Step 2: Declare performance counter and other variables and we just get average of cpu 5 times with difference of 5 seconds
     Public string GetCpuPercentage()
      {
       PerformanceCounter cpuCounter;
       double cpuUsage = 0;
       int totalCpuUsage = 0;
       double returnLoopCount = 0;
    
       cpuCounter = new PerformanceCounter();
       cpuCounter.CategoryName = "Processor";
       cpuCounter.CounterName = "% Processor Time";
       cpuCounter.InstanceName = "_Total";
       for (int i = 0; i < 5; i++)
       {
       cpuUsage += cpuCounter.NextValue();
       System.Threading.Thread.Sleep(1000);
       }
       totalCpuUsage = Convert.ToInt32(Math.Ceiling(cpuUsage / 5));
       return totalCpuUsage;
      }
    
  3. Step 3:Declare performance counter and other variables and we just get average of RAM memory usage by using 'Kernel32.dll'
     public string GetRamPercentage()
      {
          PerformanceCounter ramCounter;
               double ramUsage = 0;
               int TotalRamMemory = 0;
               int AvailableRamMemory = 0;
               int UsedRamMemory = 0;
               int RamUsagePercentage = 0;
               double returnLoopCount = 0;
               MEMORYSTATUSEX statEX = new MEMORYSTATUSEX();
               statEX.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
               GlobalMemoryStatusEx(ref statEX);
    
               double ram = (double)statEX.ullTotalPhys;
               //float ram = (float)stat.TotalPhysical;
               ram /= 1024;
               ram /= 1024;
    
               TotalRamMemory = Convert.ToInt32(Math.Round(ram, 0));
               ramCounter = new PerformanceCounter("Memory", "Available MBytes");
               for (int i = 0; i < 5; i++)
               {
                   ramUsage += ramCounter.NextValue();
                   System.Threading.Thread.Sleep(1000);
               }
               AvailableRamMemory = Convert.ToInt32(Math.Round((ramUsage / 5), 0));
               UsedRamMemory = TotalRamMemory - AvailableRamMemory;
               RamUsagePercentage = ((UsedRamMemory * 100) / TotalRamMemory);
    
       return RamUsagePercentage;
            
      }
    
         [return: MarshalAs(UnmanagedType.Bool)]
         [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
         internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
    
     [StructLayout(LayoutKind.Sequential)]
     internal struct MEMORYSTATUSEX
      {
           internal uint dwLength;
           internal uint dwMemoryLoad;
           internal ulong ullTotalPhys;
           internal ulong ullAvailPhys;
           internal ulong ullTotalPageFile;
           internal ulong ullAvailPageFile;
           internal ulong ullTotalVirtual;
           internal ulong ullAvailVirtual;
           internal ulong ullAvailExtendedVirtual;
      }
    
    

No comments:

Post a Comment