I am developing an app where I need to ping an IP address and return the status of that IP address. All of this works but its not a continuous ping and only runs once on application start.
This is the code:
private async void frmMainWindow_Shown(object sender, EventArgs e)
{
var ipAddresses = new List<string>();
var ipControls = new List<Control>();
var font = new Font("Segoe UI", 20, FontStyle.Regular, GraphicsUnit.Pixel);
string sql = "SELECT [host], [ip] FROM addhosts";
string connString = @"Data Source=PC\SQL;Initial Catalog=PingMonitorDB;Integrated Security=True";
using (var conn = new SqlConnection(connString))
using (var cmd = new SqlCommand(sql, conn))
{
await conn.OpenAsync();
using (var reader = await cmd.ExecuteReaderAsync())
{
int ipCount = 0;
while (await reader.ReadAsync())
{
var lbl = new Label()
{
AutoSize = false,
BackColor = Color.LightGray,
ForeColor = Color.Black,
Font = font,
Name = $"lblAddress{ipCount}",
Size = new Size(flowLayoutPanel1.ClientSize.Width, font.Height + 4),
Text = $"{reader["host"]} {reader["ip"]} "
};
ipAddresses.Add(reader["ip"].ToString());
ipControls.Add(lbl);
}
}
}
if (ipAddresses.Count > 0)
{
flowLayoutPanel1.Controls.AddRange(ipControls.ToArray());
var massPing = new MassPing();
var progress = SetPingProgressDelegate(ipControls);
await massPing.PingAll(ipAddresses.ToArray(), progress, 100);
}
}
This bit of coding I need to make continuous (to run every 5 seconds) is this:
if (ipAddresses.Count > 0)
{
flowLayoutPanel1.Controls.AddRange(ipControls.ToArray());
var massPing = new MassPing();
var progress = SetPingProgressDelegate(ipControls);
await massPing.PingAll(ipAddresses.ToArray(), progress, 100);
}
How can I achieve this?