RunDown BassMan

RunDown BassMan

  • NA
  • 22
  • 28.8k

C# Events and Delegates

Jan 6 2013 7:09 AM

Hey, im creating a Resource monitor program to get data on current cpu and ram usage, Im working with events to invoke the code but im having some problems understanding how it would work with my code. Here is the class -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Resource_Monitor
{
  
    class PerformanceStatistics
    {
       
        private PerformanceCounter cpuCounter;
     
        private PerformanceCounter ramCounter;

      
        public PerformanceStatistics()
        {
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            ramCounter = new PerformanceCounter("Memory", "Available MBytes");
        }

        public float GetCurrentCpuUsage()
        {
            return cpuCounter.NextValue();
        }

        public float GetCurrentAvailableRam()
        {
            return ramCounter.NextValue();
        }


        //This is my attempt of using Delegates and events, But obviously wrong
public delegate void EventHandler(object sender, System.EventArgs e);

        public event EventHandler CPU;
        CPU


        protected virtual void CPU(System.EventArgs e)
        {
            PerformanceStatistics performanceStatistics = new PerformanceStatistics();
            performanceStatistics.GetCurrentCpuUsage();
     


       

    }
}

My main window has labels called label2 and label4, im trying to get the data to appear in these labels but no luck so far. Any Help out there?

Thank you.


Answers (2)