-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
77 lines (64 loc) · 2.98 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.Speech.Synthesis;
namespace CPU_and_Memory_monitor
{
class Program
{
static void Main(string[] args)
{
#region Performance counters
// This will pull the current CPU load in percentage
PerformanceCounter perfCountCPU = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
perfCountCPU.NextValue();
// This will pull the current available memory in Megabytes
PerformanceCounter perfCountMem = new PerformanceCounter("Memory", "Available MBytes");
perfCountMem.NextValue();
// This will get us the system up time (in seconds)
PerformanceCounter perfCountUpTime = new PerformanceCounter("System", "System Up Time");
perfCountUpTime.NextValue();
#endregion
// This will greet the user in the default voice
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.Speak("This is CPU and Memory monitor");
synth.Speak("Hello, how are you today?");
synth.SelectVoiceByHints(VoiceGender.Female);
TimeSpan uptime = TimeSpan.FromSeconds(perfCountUpTime.NextValue());
string UpTimeMessage = String.Format("The current system up time is {0} days, {1} hours, {2} minutes and {3} seconds", (int)uptime.TotalDays, (int)uptime.Hours, (int)uptime.Minutes, (int)uptime.Seconds);
Console.WriteLine(UpTimeMessage);
synth.Speak(UpTimeMessage);
while (true)
{
int currentCpuPercentage = (int)perfCountCPU.NextValue();
int currentAvailableMemory = (int)perfCountMem.NextValue();
// Every 1 second print the CPU load in percentage to the screen
Console.WriteLine("CPU load %: {0}", currentCpuPercentage);
Console.WriteLine("Available Memory: {0}MB", currentAvailableMemory);
if (currentCpuPercentage > 80)
{
if (currentCpuPercentage == 100)
{
synth.SelectVoiceByHints(VoiceGender.Female);
synth.Speak("Warning, warning CPU load is 100 percent!");
}
else
{
string cpuLoadMessage = String.Format("The current CPU load is {0} percent", currentCpuPercentage);
synth.Speak(cpuLoadMessage);
}
}
if (currentAvailableMemory < 1024)
{
string MemoryMessage = String.Format("The current available memory is {0} megabytes", currentAvailableMemory);
synth.Speak(MemoryMessage);
}
Thread.Sleep(1000);
}
}
}
}