Use ping method to save it in an array c # for a game

0

How can I store all of the server's ping captures in an array.

I capture it and show it in a listbox and from there I grab the smaller and bigger ping, I put a timer to do it every second. The problem is that if you paint every second you will create a loop.

    private void timer1_Tick(object sender, EventArgs e)
    {
        for (int i = 0; i < 2; ++i)
        {
            double[] num = new double[i];

            using (Ping p = new Ping())
            {
                listBox1.Items.Add(p.Send("ip99.ip-144-217-175.net").RoundtripTime.ToString() + "ms\n");

                lblbajo.Text = listBox1.Items.Cast<string>().Min(x => Convert.ToString(x));
                lblalto.Text = listBox1.Items.Cast<string>().Max(x => Convert.ToString(x));
            }
            listBox1.Items.Clear();

        }
    }
    
asked by Francisco Gavidia 12.09.2017 в 21:08
source

1 answer

0

Greetings! The truth is very simple. The first thing is that the vector must be declared outside the cycle, and then you change the assignment of the values to the vector:

private void timer1_Tick(object sender, EventArgs e)
{
    double[] num = new double[2];
    for (int i = 0; i < 2; ++i)
    {
        using (Ping p = new Ping())
        {
            listBox1.Items.Add(p.Send("ip99.ip-144-217-175.net").RoundtripTime.ToString() + "ms\n");

            num[0] = listBox1.Items.Cast<string>().Min(x => Convert.ToString(x));
            num[1] = listBox1.Items.Cast<string>().Max(x => Convert.ToString(x));
        }
        listBox1.Items.Clear();
    }
}

In the previous case num[0] will always represent the lowest value and num[1] will always represent the highest value.

Now, since your question is resolved and is somewhat ambiguous, consider the following:

  • Call the timer on a thread ( Thred ) to prevent it from freeze the program.
  • Use conditions before replacing the values of the array, to ensure that the values are always the lowest or highest.
  • answered by 18.09.2017 в 03:28