0

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?

  • 3
    That sounds like a job for a [timer](https://stackoverflow.com/questions/186084/how-do-you-add-a-timer-to-a-c-sharp-console-application). ([winforms even has its own version](https://stackoverflow.com/questions/1142828/add-timer-to-a-windows-forms-application)) – devsmn Aug 24 '21 at 07:08
  • Please check [stackoverflow](https://stackoverflow.com/questions/6169288/execute-specified-function-every-x-seconds) post. This might help you. – Ishtiaq Aug 24 '21 at 07:32
  • Timer is a go-to choice for such scenarios but you can also use Task with a [CancellationTokenSource](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken). Task will be running every 5 seconds until the `iscancellationrequested` property of **CancellationTokenSource** object becomes `true`. Now, Its totally upto you when you have to set this property, either when a condition satisfies or throughout the application's life. – Abhinav Pandey Aug 25 '21 at 11:38

0 Answers0