Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to show CPU Usage in .Net using Threading

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 401
    Comment on it

    Hi Friends,

    Today I will tell you about how you can see CPU usage using c#.

    For this, we will use System.Diagnostics;

    The CPU Usage will include CPU usage(in %) and RAM usage(in MB)

    I have written code in a thread and I am executing the thread with 1 second of the interval so that we can get latest CPU and RAM usage.

    I have written the following code to get the CPU Usage.

    class Program
        {
            //Declare the thread timer.
            private static Timer threadtimer;
    
            //Declare the PerformanceCounter for CPU and RAM
            private static PerformanceCounter cpuCount;
            private static PerformanceCounter memoryCount;
    
            static void Main(string[] args)
            {
                //Create the instance of CPU Count
                cpuCount = new PerformanceCounter();
    
                //Intialize the categoryname,countername and instancename as per format. Don't change the format strings name
                cpuCount.CategoryName = "Processor";
                cpuCount.CounterName = "% Processor Time";
                cpuCount.InstanceName = "_Total";
    
                //Create the instance of memory count
                memoryCount = new PerformanceCounter("Memory", "Available MBytes");
    
                //Set up thread to execute the function to get CPU and RAM Usage
                threadtimer = new Timer(x => { GetUsage(); }, null, Timeout.Infinite, Timeout.Infinite);
                //Setup the thread timer
                SetupTimer();
    
                Console.ReadKey();
            }
    
            /// <summary>
            /// Get the CPU and RAM Usage
            /// </summary>
            private static void GetUsage()
            {
                Console.WriteLine("Cpu Usage = " + getCpuUsage()+ " RAM Usage = "+ getRAMUsage());
            }
    
            /// <summary>
            /// Get the latest values for CPU usage everytime thread executes.
            /// </summary>
            /// <returns></returns>
            public static string getCpuUsage()
            {
                return cpuCount.NextValue() + "%";
            }
    
            /// <summary>
            /// Get RAM usage everytime thread executes
            /// </summary>
            /// <returns></returns>
            public static string getRAMUsage()
            {
                return memoryCount.NextValue() + "MB";
            }
    
            /// <summary>
            /// Setup thread timer.
            /// </summary>
            private static void SetupTimer()
            {
                DateTime timerRunningTime = DateTime.Now.AddSeconds(1);
                double tickTime = (timerRunningTime - DateTime.Now).TotalSeconds;
                threadtimer.Change(TimeSpan.FromSeconds(tickTime), TimeSpan.FromSeconds(tickTime));
            }
        }

    If you have any query. Please mention in comments.

    Thanks For Reading

    Happy Coding!!!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: