C# CPU-Based Timer

download C# CPU-Based Timer

of 9

Transcript of C# CPU-Based Timer

  • 8/11/2019 C# CPU-Based Timer

    1/9

    NORTH CAROLINA A&T STATE UNIVERSITY

    CPU-Based TimerPervasive Computing: Spring 2014

    Take-Home Quiz 5

    Monique Kirkman-Bey

    2/24/2014

  • 8/11/2019 C# CPU-Based Timer

    2/9

    Kirkman-Bey Take-Home Quiz 5 1

    CPUBasedStopwatch Overview

    The stopwatch that follows has the ability to count to 99 minutes and 59.99 seconds. Itoperates like a typical stopwatch with the ability to start, pause, and reset. Additionally, thestopwatch utilizes context awareness and updates its display precision based on the current CPUusage. In this design, the threshold for altering the displayed precision is set to 30%. When theCPU usage is below the threshold value, the stopwatch will display the timer with 0.01 second

    precision. If the CPU usage is above the threshold, the timer will continue to maintain 0.01second precision, but will only display 0.1 second precision at the user interface.

    Additionally, the current CPU usage is displayed in two fashions - continuously andinstantaneously. The continuously monitored value is displayed and updated in real-time.However, the dynamic changes in the CPU usage lead to rapidly changing values that aredifficult to read. In the event the user needs to know the CPU usage, the user simply needs toclick the Get Instantaneous CPU Usage button. This action will capt ure the current usage

    value, and display that value until the button is pressed again. It allows the user an opportunity tosee the usage long enough to be read by the human eye. It is important to note that this value isonly updated when the Get Instantaneous CPU Usage button is pressed.

  • 8/11/2019 C# CPU-Based Timer

    3/9

    Kirkman-Bey Take-Home Quiz 5 2

    CPUBasedTimer Operation and GUI Screenshots

    When the CPUBasedTimer program is loaded, the user is presented with the GUI shown inFigure 1. The timer display format is Minutes:Seconds:Centiseconds . You can see that thereare 2 buttons to control the timer: Start and Reset. To the right of the timer, the user can monitorthe usage in two ways as explained above. It can also be seen that there is no value displayed forinstantaneous usage because the Get Instantaneous CPU Usage button has not been pressed

    Figure 1: CPUBasedTimer GUI

    The timer can also be paused once started. After the Start button is pressed, the text updates andit becomes the Stop button, as shown below in Figure 2. Pressing the Stop button will cause thetimer to pause.

    Figure 2: Pausing the CPUBasedTimer Program

    After pressing the Get Instantaneous CPU Usage button, additional text is visible below the button which displays the instantaneous usage of the CPU. This can be seen below in Figure 3.Please note that the instantaneous usage was, in-fact, 0% at this point even though the counter iscurrently running. This is confirmed by the windows task manager as shown next to theCPUBasedTimer window.

  • 8/11/2019 C# CPU-Based Timer

    4/9

    Kirkman-Bey Take-Home Quiz 5 3

    Figure 3: Displaying Instantaneous CPU Usage

    To increase the usage, a 30-second loop was implemented and published. After implementing theloop, CPU usage for my virtual machine jumped to 100%. It is important to note here that theVisual Studio software is implemented in a virtual machine with expanding resources. So, thetask manager monitors the CPU usage of the virtual machine, and not necessarily the CPU usageof the physical mac on which the virtual machine is run. The 100% CPU usage value caused thedisplay precision to decrease due to its being above the 30% threshold. Hence, in Figure 4 belowyou will see that both an updated user message and updated timer precision are displayed.Additionally, the task manager usage is displayed alongside the timer to confirm accurate CPUusage monitoring.

    Figure 4: Display Precision Decrease Due to CPU Usage Above 30%

  • 8/11/2019 C# CPU-Based Timer

    5/9

    Kirkman-Bey Take-Home Quiz 5 4

    CPUBasedStopwatch UML Class Diagram

    In Figure 5 below, the UML Class Diagram for the CPUBasedStopwatch program isdisplayed. It can be seen that the program contains single class, called Form1. This class has thefollowing attributes: TenthSecond, HundredthSecond, Sec1, Sec10, Min1, Min10, usage,HighUsage, digits, Start_Stop. The first six attributes correspond to the respective labels thatdisplay the values of the counter as described by the attribute names. The usage variable is usedto store the value of the continuously monitored CPU usage. HighUsage is a flag that is eithertrue or false if the threshold value has been exceeded or not, respectively. Digits is a string arraythat holds the values 1-10 for quicker conversion between int and string when the values need to

    be displayed on the labels. Lastly, Start_Stop is a boolean flag that allows the program todetermine if the start button or stop button has been clicked.

    This class has 4 methods and a constructor. Form1 is simply the constructor of the class.Timer1_tick handles the behavior of the timer, the timer labels, and the continuously monitoredCPU usage variable. Start_Stop_Button_Click responds to the Start or Stop button being clickedand sets the Start_Stop flag accordingly. Button1_click allows the instantaneous CPU usage to

    be obtained when desired. Lastly, the ResetButton_Click method stops and resets the stopwatchwhen the Reset button is clicked.

    Please note that hidden classes, such as the Program class which implements the Main()function, and the resources and setting class are not displayed in this diagram because they arecomputer generated classes.

    +Form1()+timer1_Tick(in sender : object, in e :EventArgs)-Start_Stop_Button_Click(in sender : object, in e : EventArgs)-button1_Click(in sender : object, in e :EventArgs)-ResetButton_Click(in sender : object, in e : EventArgs)

    -TenthSecond : int-HundredthSecond : int-Sec1 : int-Sec10 : int-Min1 : int-Min10 : int-usage : int-HighUsage : bool-digits : string-Start_Stop : bool

    Form1

    Figure 5: Simple UML Class Diagram

    A more complete UML Class Diagram, including hidden objects and attributes, wasgenerated in Visual Studio 2013 and is displayed in Figure 6.

  • 8/11/2019 C# CPU-Based Timer

    6/9

    Kirkman-Bey Take-Home Quiz 5 5

    Figure 6: Visual Studio 2013 Generated UML Class Diagram

  • 8/11/2019 C# CPU-Based Timer

    7/9

    Kirkman-Bey Take-Home Quiz 5 6

    CPUBasedStopwatch C# code:

    using System;using System.Collections.Generic; using System.ComponentModel;using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics;

    namespace CPUBasedStopwatch{

    public partial class Form1 : Form //Class {

    //Below, each digit is controlled individually within the CPUBased timer int TenthSecond; int HundredthSecond;

    int Sec1;int Sec10;int Min1;int Min10;int usage;bool HighUsage = false ; //True when CPU Usage > 30%

    /*you will notice that the number 10 is included within the string array, although itis never displayed. This was done to overcome the error that was thrown when testingthe condition "if (integer == 10)"*/string [] digits = { "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" }; bool Start_Stop = false ; //True when Start is pressed, False when stop is pressed

    public Form1() //Constructor {

    InitializeComponent();}

    public void timer1_Tick( object sender, EventArgs e) //Method {

    /*this first sequence of code allows me to convert the string currently beingdisplayed in the respective label to integer values so that they can beincremented as they would in a numerical system*/ HundredthSecond = Convert .ToInt16(HundredthSecondLabel.Text);TenthSecond = Convert . ToInt16(TenthSecondLabel.Text);Sec1 = Convert .ToInt16(SecondsOnesLabel.Text);Sec10 = Convert .ToInt16(SecondsTensLabel.Text);Min1 = Convert .ToInt16(MinutesOnesLabel.Text);Min10 = Convert .ToInt16(MinutesTensLabel.Text);

    //The value of the Performance counter is assigned to usage usage = ( int )PerformanceCounter1.NextValue();

    //These conditions allow the precision to be updated based on the 30% threshold if (usage > 30){

    HighUsage = true ; HundredthSecondLabel.Visible = false ;label1.Text = "CPU Usage above 30%. Timer precision reduced to 0.1 second .";

    }

  • 8/11/2019 C# CPU-Based Timer

    8/9

    Kirkman-Bey Take-Home Quiz 5 7

    else if (usage < 30){

    HighUsage = false ;HundredthSecondLabel.Visible = true ;label1.Text = "CPU Usage below 30%. 0.01 second timer precision available .";

    }

    //Displays the CPU Usage in label3 label3.Text = "Continuously Monitored: " + usage + "% CPU Usage" ;

    /*if the start button is pressed, increment the counters as follows. When thestop button is pressed, do not increment the counters*/ if (Start_Stop == true ){

    if (++HundredthSecond == 10 ){

    HundredthSecond = 0;TenthSecond++;

    if (TenthSecond == 10 ){

    TenthSecond = 0;Sec1++;

    if (Sec1 == 10){

    Sec1 = 0;Sec10++;

    if (Sec10 == 6){

    Sec10 = 0;Min1++;

    if (Min1 == 10 ){

    Min1 = 0;Min10++;

    if (Min10 == 10 ){

    Start_Stop = false ;Start_Stop_Button.Text = "Start ";HundredthSecond = 0;TenthSecond = 0;Sec1 = 0;Sec10 = 0;Min1 = 0;Min10 = 0;

    }

    }}

    }}

    }}

    /*the following code allows the integers to quickly be converted to stringsavailable in the string array. This method is faster than the simpleconvert.string method because there is a much smaller string array when

  • 8/11/2019 C# CPU-Based Timer

    9/9

    Kirkman-Bey Take-Home Quiz 5 8

    selecting the appropriate string value*/ HundredthSecondLabel.Text = digits[HundredthSecond];TenthSecondLabel.Text = digits[TenthSecond];SecondsOnesLabel.Text = digits[Sec1];SecondsTensLabel.Text = digits[Sec10];MinutesOnesLabel.Text = digits[Min1];MinutesTensLabel.Text = digits[Min10];

    }

    private void Start_Stop_Button_Click( object sender, EventArgs e) //Method {

    //if start/stop button pressed, negate the value of the start_stop "flag" Start_Stop = !Start_Stop;

    /*update the text in the start_stop_button to reflect the action that will beexecuted if the button is clicked again*/ if (Start_Stop_Button.Text == "Start ")

    Start_Stop_Button.Text = "Stop" ; else if (Start_Stop_Button.Text == "Stop ")

    Start_Stop_Button.Text = "Start ";}

    //This handler responds to the "Get Instantaneous CPU Usage" button click public void button1_Click( object sender, EventArgs e) //Method {

    /*a second Performance Counter, with the same parameters as that of the firstis implemented */ int usage2;usage2 = ( int )PerformanceCounter2.NextValue(); label2.Text = "Instantaneuous Usage: " + usage2 + " % CPU Usage" ;

    }

    /*This handler responds to the reset button click. It stops the stopwatch andresets the stopwatch to its inital state */ private void ResetButton_Click( object sender, EventArgs e) //Method {

    Start_Stop = false ;Start_Stop_Button.Text = "Start" ;HundredthSecond = 0;TenthSecond = 0;Sec1 = 0;Sec10 = 0;Min1 = 0;Min10 = 0;

    HundredthSecondLabel.Text = digits[HundredthSecond];TenthSecondLabel.Text = digits[TenthSecond];SecondsOnesLabel.Text = digits[Sec1];

    SecondsTensLabel.Text = digits[Sec10];MinutesOnesLabel.Text = digits[Min1];MinutesTensLabel.Text = digits[Min10];

    }}

    }