Skip to content

Commit

Permalink
Minor Improvments and additions
Browse files Browse the repository at this point in the history
  • Loading branch information
SiliconSquire committed Sep 13, 2023
1 parent b5d3981 commit cc0ef0d
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 338 deletions.
3 changes: 3 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<setting name="AutoPingEnabled" serializeAs="String">
<value>True</value>
</setting>
<setting name="LastUsedIpAddress" serializeAs="String">
<value>8.8.8.8</value>
</setting>
</Pingerino.Properties.Settings>
</userSettings>
</configuration>
481 changes: 260 additions & 221 deletions FormPinger.Designer.cs

Large diffs are not rendered by default.

204 changes: 140 additions & 64 deletions FormPinger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public partial class FormPinger : Form
private readonly System.Timers.Timer cpuTimer;
private readonly System.Timers.Timer ramTimer;
private readonly System.Threading.Timer pingTimer;
private string currentIpAddress = "8.8.8.8";
private string currentIpAddress = Properties.Settings.Default.LastUsedIpAddress;
private readonly List<long> pingRoundTripTimes = new List<long>();
private readonly List<double> packetLossValues = new List<double>();
private readonly List<double> jitterValues = new List<double>();
Expand All @@ -37,8 +37,7 @@ public partial class FormPinger : Form
private readonly WinForms.Button ButtonCleanTemp;
private readonly BackgroundWorker networkWorker;
private readonly WinForms.ProgressBar progressBarNetwork;
private const int MaxLines = 999;
private readonly CircularBuffer<string> outputLines = new CircularBuffer<string>(MaxLines);
private readonly List<string> outputLines = new List<string>();


[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
Expand Down Expand Up @@ -152,18 +151,21 @@ public FormPinger()

private void InitializeContextMenu()
{
contextMenuStrip1 = new ContextMenuStrip(); // Change this line
contextMenuStrip1.Items.Add("[1] Run with Windows");
contextMenuStrip1 = new ContextMenuStrip();
contextMenuStrip1.Items.Add("[1] Show Network Details");
contextMenuStrip1.Items.Add("[2] Auto Ping");
contextMenuStrip1.Items.Add("[3] Run with Windows");


ButtonMenu.Click += (sender, e) =>
{
contextMenuStrip1.Show(ButtonMenu, 0, ButtonMenu.Height);
};

// Attach event handlers to the menu items
contextMenuStrip1.Items[0].Click += Option1_Click;
contextMenuStrip1.Items[1].Click += Option2_Click;
contextMenuStrip1.Items[0].Click += ShowNetworkDetails_Click;
contextMenuStrip1.Items[1].Click += Option1_Click;
contextMenuStrip1.Items[2].Click += Option2_Click;
}


Expand All @@ -186,12 +188,44 @@ private void FormPinger_FormClosing(object sender, FormClosingEventArgs e)
}


private void ShowNetworkDetails_Click(object sender, EventArgs e)
{
string details = "";

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties properties = nic.GetIPProperties();

details += $"Description: {nic.Description}\n";
details += $"Physical Address: {nic.GetPhysicalAddress()}\n";
details += $"DHCP Enabled: {(properties.GetIPv4Properties().IsDhcpEnabled ? "Yes" : "No")}\n";

foreach (UnicastIPAddressInformation ip in properties.UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // IPv4 addresses only
{
details += $"IPv4 Address: {ip.Address}\n";
details += $"IPv4 Subnet Mask: {ip.IPv4Mask}\n";
}
}

foreach (GatewayIPAddressInformation gateway in properties.GatewayAddresses)
{
details += $"IPv4 Default Gateway: {gateway.Address}\n";
}

details += $"IPv4 DHCP Server: {properties.DhcpServerAddresses.FirstOrDefault()}\n";
details += $"IPv4 DNS Servers: {string.Join(", ", properties.DnsAddresses)}\n";
}
}

MessageBox.Show(details);
}

private void FormPinger_Load(object sender, EventArgs e)
{
// Update network statistics when the app is opened
// UpdateNetworkStatistics();
{

// Load the form's position
if (Properties.Settings.Default.FormPosition != null)
Expand Down Expand Up @@ -312,8 +346,10 @@ private void AddLineToOutput(string Time, string Status, string RTT = "")
else
{
dataGridView1.Rows.Add(new object[] { Time, Status, RTT });
outputLines.Add($"{Time} - {Status} - {RTT}"); // Store to CircularBuffer
outputLines.Add($"{Time} - {Status} - {RTT}");
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;


}
}
}
Expand Down Expand Up @@ -341,57 +377,44 @@ private void UpdateSmootherTextOutput(string message)
row.Cells["Time"].Value = Time;
row.Cells["Status"].Value = Status;
row.Cells["RTT"].Value = RTT;

}
else
{
dataGridView1.Rows.Add(new object[] { Time, message, string.Empty });
}

}
}

public class CircularBuffer<T>
{
private readonly T[] buffer;
private int head;
private int tail;

public int MaxSize { get; private set; }


public CircularBuffer(int size)
private void TextBoxInterval_KeyDown(object sender, KeyEventArgs e)
{
// Check if the user pressed the Enter key
if (e.KeyCode == Keys.Enter)
{
buffer = new T[size];
MaxSize = size;
}
// Clear collections
pingRoundTripTimes.Clear();
jitterValues.Clear();
packetLossValues.Clear();

public void Add(T item)
{
buffer[head] = item;
head = (head + 1) % MaxSize;
if (head == tail)
// Reset statistics display
if (dataGridViewPing.Rows.Count > 0)
{
tail = (tail + 1) % MaxSize; // remove oldest item
dataGridViewPing.Rows[0].Cells["MaxPing"].Value = "0 ms";
dataGridViewPing.Rows[0].Cells["MinPing"].Value = "0 ms";
dataGridViewPing.Rows[0].Cells["AvgPing"].Value = "0 ms";
}
}

public T[] ToArray()
{
if (head < tail)
{
return buffer.Skip(tail).Concat(buffer.Take(head)).ToArray();
}
else
{
return buffer.Skip(tail).Take(head - tail).ToArray();
}
}
}
TextBoxMaxJitter.Text = "0 ms";
TextBoxMinJitter.Text = "0 ms";
TextBoxAvgJitter.Text = "0 ms";

TextBoxMaxLoss.Text = "0%";
TextBoxMinLoss.Text = "0%";
TextBoxAvgLoss.Text = "0%";

private void TextBoxInterval_KeyDown(object sender, KeyEventArgs e)
{
// Check if the user pressed the Enter key
if (e.KeyCode == Keys.Enter)
{
// Parse the interval from the TextBox control
if (int.TryParse(textBoxInterval.Text, out int interval))
{
Expand All @@ -406,8 +429,32 @@ private void TextBoxIpAddress_KeyDown(object sender, KeyEventArgs e)
// Check if the user pressed the Enter key
if (e.KeyCode == Keys.Enter)
{
// Clear collections
pingRoundTripTimes.Clear();
jitterValues.Clear();
packetLossValues.Clear();

// Reset statistics display
if (dataGridViewPing.Rows.Count > 0)
{
dataGridViewPing.Rows[0].Cells["MaxPing"].Value = "0 ms";
dataGridViewPing.Rows[0].Cells["MinPing"].Value = "0 ms";
dataGridViewPing.Rows[0].Cells["AvgPing"].Value = "0 ms";
}

TextBoxMaxJitter.Text = "0 ms";
TextBoxMinJitter.Text = "0 ms";
TextBoxAvgJitter.Text = "0 ms";

TextBoxMaxLoss.Text = "0%";
TextBoxMinLoss.Text = "0%";
TextBoxAvgLoss.Text = "0%";

// Change the IP address to ping
currentIpAddress = ipAddress.Text;
// Save the updated IP address to settings
Properties.Settings.Default.LastUsedIpAddress = currentIpAddress;
Properties.Settings.Default.Save();
}
}

Expand Down Expand Up @@ -508,6 +555,9 @@ private void UpdatePingStatistics()

// Resize the row
dataGridViewPing.AutoResizeRow(0);

dataGridViewPing.ClearSelection();
dataGridViewPing.CurrentCell = null;
});
}
}
Expand All @@ -519,7 +569,7 @@ private void UpdatePingStatistics()
private void UpdateOption1Text(string Pingerino)
{
bool isTaskInScheduler = IsTaskInScheduler(Pingerino);
contextMenuStrip1.Items[0].Text = isTaskInScheduler ? "[1] Run with Windows ✔" : "[1] Run with Windows";
contextMenuStrip1.Items[2].Text = isTaskInScheduler ? "[1] Run with Windows ✔" : "[1] Run with Windows";
}


Expand Down Expand Up @@ -763,11 +813,6 @@ private void COPYRIGHT_TextChanged(object sender, EventArgs e)
// Placeholder
}

private void Close_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void Label1_Click(object sender, EventArgs e)
{
// Placeholder
Expand All @@ -778,15 +823,7 @@ private void LabelJitter_Click(object sender, EventArgs e)
// Placeholder
}

private void Close_MouseEnter(object sender, EventArgs e)
{
close.BackColor = Color.Red;
}

private void Close_MouseLeave(object sender, EventArgs e)
{
close.BackColor = Color.Transparent;
}

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
Expand All @@ -808,10 +845,6 @@ private void Label3_MouseDown(object sender, MouseEventArgs e)
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}

private void Bt_mini_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}

private void Label2_Click(object sender, EventArgs e)
{
Expand Down Expand Up @@ -1064,6 +1097,49 @@ private void ButtonMenu_Click(object sender, EventArgs e)
contextMenuStrip1.Show(ButtonMenu, 0, ButtonMenu.Height);
}




#endregion

private void ButtonExit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void ButtonMinimize_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}

private void ButtonClearAll_Click(object sender, EventArgs e)
{
// 1. Clear the underlying data lists
pingRoundTripTimes.Clear();
packetLossValues.Clear();
jitterValues.Clear();
outputLines.Clear();

// 2. Clear UI elements
dataGridView1.Rows.Clear();

// 3. Optionally, clear statistics shown in UI:
// Reset any displayed ping, jitter, or packet loss statistics (depending on your UI structure).
if (dataGridViewPing.Rows.Count > 0)
{
dataGridViewPing.Rows[0].Cells["MaxPing"].Value = "0 ms";
dataGridViewPing.Rows[0].Cells["MinPing"].Value = "0 ms";
dataGridViewPing.Rows[0].Cells["AvgPing"].Value = "0 ms";
}

TextBoxMaxJitter.Text = "0 ms";
TextBoxMinJitter.Text = "0 ms";
TextBoxAvgJitter.Text = "0 ms";

TextBoxMaxLoss.Text = "0%";
TextBoxMinLoss.Text = "0%";
TextBoxAvgLoss.Text = "0%";
}

}
}
42 changes: 21 additions & 21 deletions FormPinger.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="Time.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Status.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="RTT.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="MinPing.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="AvgPing.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="MaxPing.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="LOGOCF.ErrorImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down Expand Up @@ -239,27 +260,6 @@
XBNRXBNRXBNRXBNRXA+bzf9HN9uC5vHcuwAAAABJRU5ErkJggg==
</value>
</data>
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="Time.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Status.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="RTT.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="MinPing.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="AvgPing.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="MaxPing.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>39</value>
</metadata>
Expand Down
Loading

0 comments on commit cc0ef0d

Please sign in to comment.