Загрузка данных
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApp34
{
public partial class Form1 : Form
{
private Thermometer thermometer;
private CpuMonitor cpuMonitor;
private Timer updateTimer;
private Chart cpuChart;
private ListBox eventLog;
private Label cpuLabel;
private Label statusLabel;
private Button startButton;
private Button stopButton;
private Button clearButton;
private List<int> history;
private bool monitoring = false;
public Form1()
{
InitializeComponent();
history = new List<int>();
CreateInterface();
CreateMonitor();
SubscribeEvents();
StartMonitoring();
}
private void CreateInterface()
{
this.Text = "CPU Monitor - Laboratory 5";
this.Size = new Size(1000, 650);
this.StartPosition =
FormStartPosition.CenterScreen;
this.BackColor =
Color.FromArgb(25, 25, 40);
this.ForeColor = Color.White;
this.FormBorderStyle =
FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
thermometer = new Thermometer();
thermometer.Location = new Point(30, 70);
thermometer.Size = new Size(200, 380);
this.Controls.Add(thermometer);
cpuLabel = new Label();
cpuLabel.Text = "CPU: 0%";
cpuLabel.Font =
new Font("Arial", 20, FontStyle.Bold);
cpuLabel.ForeColor = Color.LimeGreen;
cpuLabel.Location = new Point(270, 30);
cpuLabel.Size = new Size(300, 45);
this.Controls.Add(cpuLabel);
statusLabel = new Label();
statusLabel.Text = "Статус: остановлен";
statusLabel.Location =
new Point(270, 85);
statusLabel.Size = new Size(350, 30);
statusLabel.Font =
new Font("Arial", 11);
this.Controls.Add(statusLabel);
startButton = new Button();
startButton.Text = "СТАРТ";
startButton.Location =
new Point(30, 480);
startButton.Size = new Size(120, 45);
startButton.BackColor = Color.Green;
startButton.ForeColor = Color.White;
this.Controls.Add(startButton);
stopButton = new Button();
stopButton.Text = "СТОП";
stopButton.Location =
new Point(160, 480);
stopButton.Size = new Size(120, 45);
stopButton.BackColor = Color.DarkRed;
stopButton.ForeColor = Color.White;
this.Controls.Add(stopButton);
clearButton = new Button();
clearButton.Text = "ОЧИСТИТЬ";
clearButton.Location =
new Point(290, 480);
clearButton.Size = new Size(140, 45);
this.Controls.Add(clearButton);
cpuChart = new Chart();
cpuChart.Location =
new Point(270, 130);
cpuChart.Size = new Size(680, 300);
cpuChart.BackColor =
Color.FromArgb(35, 35, 55);
ChartArea area = new ChartArea("CPU");
area.BackColor =
Color.FromArgb(35, 35, 55);
area.AxisY.Minimum = 0;
area.AxisY.Maximum = 100;
area.AxisY.Interval = 20;
area.AxisX.LabelStyle.ForeColor =
Color.White;
area.AxisY.LabelStyle.ForeColor =
Color.White;
area.AxisX.LineColor = Color.Gray;
area.AxisY.LineColor = Color.Gray;
area.AxisX.MajorGrid.LineColor =
Color.FromArgb(60, 60, 80);
area.AxisY.MajorGrid.LineColor =
Color.FromArgb(60, 60, 80);
cpuChart.ChartAreas.Add(area);
Series series = new Series("CPU Load");
series.ChartType =
SeriesChartType.Spline;
series.Color = Color.LimeGreen;
series.BorderWidth = 3;
series.ChartArea = "CPU";
cpuChart.Series.Add(series);
this.Controls.Add(cpuChart);
Label logTitle = new Label();
logTitle.Text = "ЖУРНАЛ СОБЫТИЙ";
logTitle.Location =
new Point(460, 450);
logTitle.Size = new Size(300, 25);
logTitle.Font =
new Font("Arial", 11, FontStyle.Bold);
this.Controls.Add(logTitle);
eventLog = new ListBox();
eventLog.Location =
new Point(460, 480);
eventLog.Size = new Size(490, 110);
eventLog.BackColor =
Color.FromArgb(35, 35, 55);
eventLog.ForeColor = Color.White;
this.Controls.Add(eventLog);
}
private void CreateMonitor()
{
cpuMonitor = new CpuMonitor();
updateTimer = new Timer();
updateTimer.Interval = 1000;
updateTimer.Tick += UpdateTimer_Tick;
}
private void SubscribeEvents()
{
thermometer.LoadChanged +=
Thermometer_LoadChanged;
thermometer.HighLoad +=
Thermometer_HighLoad;
thermometer.ThermometerClick +=
Thermometer_Click;
startButton.Click +=
StartButton_Click;
stopButton.Click +=
StopButton_Click;
clearButton.Click +=
ClearButton_Click;
}
private void StartMonitoring()
{
if (monitoring)
return;
monitoring = true;
cpuMonitor.GetCpuLoad();
updateTimer.Start();
statusLabel.Text =
"Статус: мониторинг запущен";
AddLog("Мониторинг запущен");
}
private void StopMonitoring()
{
if (!monitoring)
return;
monitoring = false;
updateTimer.Stop();
statusLabel.Text =
"Статус: мониторинг остановлен";
AddLog("Мониторинг остановлен");
}
private void UpdateTimer_Tick(
object sender,
EventArgs e)
{
if (!monitoring)
return;
int load = cpuMonitor.GetCpuLoad();
thermometer.Value = load;
history.Add(load);
if (history.Count > 60)
history.RemoveAt(0);
UpdateChart();
}
private void UpdateChart()
{
Series series =
cpuChart.Series["CPU Load"];
series.Points.Clear();
for (int i = 0; i < history.Count; i++)
{
series.Points.AddXY(i, history[i]);
}
}
private void Thermometer_LoadChanged(
object sender,
LoadChangedEventArgs e)
{
cpuLabel.Text = "CPU: " + e.Load + "%";
if (e.Load < 40)
{
cpuLabel.ForeColor = Color.LimeGreen;
}
else if (e.Load < 80)
{
cpuLabel.ForeColor = Color.Orange;
}
else
{
cpuLabel.ForeColor = Color.Red;
}
}
private void Thermometer_HighLoad(
object sender,
LoadChangedEventArgs e)
{
AddLog(
"ВНИМАНИЕ! Высокая загрузка CPU: "
+ e.Load + "%"
);
System.Media.SystemSounds.Exclamation.Play();
}
private void Thermometer_Click(
object sender,
EventArgs e)
{
MessageBox.Show(
"Текущая загрузка процессора: "
+ thermometer.Value + "%",
"Информация о процессоре",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
private void StartButton_Click(
object sender,
EventArgs e)
{
StartMonitoring();
}
private void StopButton_Click(
object sender,
EventArgs e)
{
StopMonitoring();
}
private void ClearButton_Click(
object sender,
EventArgs e)
{
history.Clear();
cpuChart.Series["CPU Load"].Points.Clear();
eventLog.Items.Clear();
AddLog("История очищена");
}
private void AddLog(string message)
{
string time =
DateTime.Now.ToString("HH:mm:ss");
eventLog.Items.Insert(
0,
"[" + time + "] " + message
);
if (eventLog.Items.Count > 100)
eventLog.Items.RemoveAt(
eventLog.Items.Count - 1
);
}
protected override void OnFormClosing(
FormClosingEventArgs e)
{
if (updateTimer != null)
{
updateTimer.Stop();
updateTimer.Dispose();
}
if (cpuMonitor != null)
{
cpuMonitor.Dispose();
}
base.OnFormClosing(e);
}
}
}