Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
BeratARPA committed Aug 4, 2024
1 parent d500a42 commit 7159e01
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 11 deletions.
117 changes: 106 additions & 11 deletions ServiceCheck/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions ServiceCheck/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.ServiceProcess;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -19,10 +20,16 @@ public partial class MainForm : Form
{
ConcurrentDictionary<string, (int, int, bool, bool, bool)> ServicesInFlowLayoutPanel = new ConcurrentDictionary<string, (int, int, bool, bool, bool)>();
private NotificationHelper _notificationHelper;
private PerformanceCounter cpuCounter;
private PerformanceCounter diskCounter;
private PerformanceCounter networkCounter;
private Timer timer;

public MainForm()
{
InitializeComponent();
InitializePerformanceCounters();
InitializeTimer();

GlobalVariables.MainForm = this;

Expand All @@ -35,6 +42,91 @@ public MainForm()
labelStatus.Text = "";
}

#region PerformanceCounter
private async Task InitializePerformanceCounters()
{
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
diskCounter = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
string networkName = await GetNetworkInterface();
networkCounter = new PerformanceCounter("Network Interface", "Bytes Total/sec", networkName);

// Perform initial reads to initialize counters
cpuCounter.NextValue();
diskCounter.NextValue();
networkCounter.NextValue();
}

private void InitializeTimer()
{
timer = new Timer();
timer.Interval = 250;
timer.Tick += async (sender, e) => await Timer_TickAsync();
timer.Start();
}

private async Task<string> GetNetworkInterface()
{
return await Task.Run(() =>
{
var networkInterfaces = new ManagementObjectSearcher("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface");
foreach (var networkInterface in networkInterfaces.Get())
{
return networkInterface["Name"].ToString();
}
return string.Empty;
});
}

private async Task<float> GetMemoryUsageAsync()
{
return await Task.Run(() =>
{
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (var obj in searcher.Get())
{
float totalVisibleMemory = float.Parse(obj["TotalVisibleMemorySize"].ToString()) / 1024; // MB to GB
float freePhysicalMemory = float.Parse(obj["FreePhysicalMemory"].ToString()) / 1024; // MB to GB
float usedMemory = totalVisibleMemory - freePhysicalMemory;
float memoryUsagePercentage = (usedMemory / totalVisibleMemory) * 100;
return memoryUsagePercentage;
}
return 0;
});
}

private async Task Timer_TickAsync()
{
await UpdatePerformanceDataAsync();
}

private async Task UpdatePerformanceDataAsync()
{
try
{
// CPU Kullanımı
float cpuUsage = cpuCounter.NextValue();
progressBarCPU.Value = (int)cpuUsage;
labelCPU.Text = $"CPU: {cpuUsage:F2}%";

// RAM Kullanımı
float ramUsage = await GetMemoryUsageAsync();
progressBarRAM.Value = (int)ramUsage;
labelRAM.Text = $"RAM: {ramUsage:F2}%";

// Disk Kullanımı
float diskUsage = diskCounter.NextValue();
progressBarDisk.Value = (int)diskUsage;
labelDisk.Text = $"Disk: {diskUsage:F2}%";

// Ağ Kullanımı
float networkUsage = networkCounter.NextValue() / (1024 * 1024); // MB/s
progressBarNetwork.Value = (int)networkUsage;
labelNetwork.Text = $"Ağ: {networkUsage:F2} MB/s";
}
catch { }
}
#endregion

private void LoadServicesIntoComboBox()
{
try
Expand Down
1 change: 1 addition & 0 deletions ServiceCheck/ServiceCheck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down

0 comments on commit 7159e01

Please sign in to comment.