load current cpu in percentage
static void Main (string[] args) {
PerformanceCounter cpuCounter1 = new PerformanceCounter("Processor", "% Processor Time", "_Total");
PerformanceCounter cpuCounter2 = new PerformanceCounter("Processor", "% Privileged Time", "_Total");
PerformanceCounter cpuCounter3 = new PerformanceCounter("Processor", "% User Time", "_Total");
while (true)
{
Console.WriteLine($" CPU_Processor Time :\t {cpuCounter1.NextValue()} %");
Console.WriteLine($" CPU_Privileged Time :\t {cpuCounter2.NextValue()} %");
Console.WriteLine($" CPU_User Time :\t {cpuCounter3.NextValue()} %");
Console.WriteLine();
Thread.Sleep(1000);
}
}
load current available memory in Megabytes - Important Memory Counters
static void Main (string[] args) {
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
PerformanceCounter privateBytes = new PerformanceCounter("Process", "Private Bytes", currentProcess);
PerformanceCounter gen2Collections = new PerformanceCounter(".NET CLR Memory", "# Gen 2 Collections", currentProcess);
while (true)
{
Console.WriteLine($" RAM_Available GBytes:\t {ramCounter.NextValue()/1024} GB");
Console.WriteLine($" private bytes :\t {privateBytes.NextValue()}");
Console.WriteLine($" gen 2 collections :\t {gen2Collections.NextValue()}");
Console.WriteLine();
Thread.Sleep(1000);
}
}
public partial class MainWindow : Window
{
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
public MainWindow()
{
InitializeComponent();
System.Windows.Threading.DispatcherTimer dispatcherTimer1 = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer1.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer1.Tick += timer_Tick;
dispatcherTimer1.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
txtboxRam.Text = (ramCounter.NextValue() / 1024).ToString("F");
txtboxCpu.Text = cpuCounter.NextValue().ToString();
if ((ramCounter.NextValue() / 1024) < 6)
{
txtboxRam.Background = Brushes.Red;
}
else
{
txtboxRam.Background = Brushes.White;
}
}
}
WPF
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="AliceBlue">
<TextBlock Text="RAM & CPU Usage" Margin="15 40 0 0" FontSize="18" FontWeight="Bold" />
</StackPanel>
<Grid Grid.Row="1" Margin="15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160"/>
<ColumnDefinition Width="160"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Available Memory" Margin="0 13" FontSize="18" Grid.Row="0" HorizontalAlignment="Center"/>
<Label Content="CPU" Margin="0 13" FontSize="18" Grid.Row="1" HorizontalAlignment="Center"/>
<TextBox x:Name="txtboxRam" Grid.Row="0" Grid.Column="1" Height="30" FontSize="20" VerticalContentAlignment="Center"/>
<TextBox x:Name="txtboxCpu" Grid.Row="1" Grid.Column="1" Height="30" FontSize="20" VerticalContentAlignment="Center"/>
<Label Content="Gegabytes" Margin="0 13" FontSize="16" Grid.Row="0" Grid.Column="2"/>
<Label Content="%" Margin="0 13" FontSize="16" Grid.Row="1" Grid.Column="2"/>
</Grid>
</Grid>
Some Exeptions types
static void Main (string[] args) {
try
{
Console.Write("Enter First Number: ");
int Numerator = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Second Number: ");
int Denominator = Convert.ToInt32(Console.ReadLine());
int Result = Numerator / Denominator;
Console.WriteLine("Result = {0}", Result);
}
catch (DivideByZeroException)
{
Console.WriteLine("DivideByZeroException: Denominator cannot be zero");
}
catch (FormatException)
{
Console.WriteLine("FormatException: Please enter a number");
}
catch (OverflowException)
{
Console.WriteLine($"OverflowException: only number {Int32.MinValue} && {Int32.MaxValue} are allowed");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
HTML
<div>
<span id="hours">00</span> :
<span id="minutes">00</span> :
<span id="seconds">00</span>
</div>
JavaScript
function add_leading_zero(number) {
if (number < 10) {
return "0" + number.toString();
} else {
return number.toString();
}
}
window.setInterval(function () {
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
document.getElementById("hours").innerHTML = add_leading_zero(hours);
document.getElementById("minutes").innerHTML = add_leading_zero(minutes);
document.getElementById("seconds").innerHTML = add_leading_zero(seconds);
}, 1000);
-