简体   繁体   中英

How to make run real time and faster refresh method with timer

I have script for refresh network with object label and panel but in script using looping mode with 'for'. I want to this real time refresh for 1 sec or 5 sec but because using 'for' make this procces need more time and get stuck screen. how to make the solution more quickly and in real time? Thanks

        public PosPing()
        {
            InitializeComponent();
            RefreshPOS.Tick += new EventHandler(CheckPOSUG);
            RefreshPOS.Start();

        }

        private void CheckPOSUG(object sender, EventArgs e) 
        {
            Panel[] panelUG = new Panel[]{pnlPOSUG1,pnlPOSUG2,pnlPOSUG3,pnlPOSUG4,pnlPOSUG5,pnlPOSUG6,pnlPOSUG7,pnlPOSUG8};
            Label[] LabelUG = new Label[]{lblUG1,lblUG2,lblUG3,lblUG4,lblUG5,lblUG6,lblUG7,lblUG8};
            Label[] lblSpdUG = new Label[] { lblSpdUG1, lblSpdUG2, lblSpdUG3, lblSpdUG4, lblSpdUG5, lblSpdUG6, lblSpdUG7, lblSpdUG8 };

            for (int x = 0; x < 8;x++ )
            {
                string IP = "192.168.135.1" + (x + 1).ToString();
                var ping = new Ping();
                var reply = ping.Send(IP, 10 * 1000);
                LabelUG[x].Text = "POSBMS10" + x.ToString();

                if (reply.Status == IPStatus.Success)
                {
                    lblSpdUG[x].Text = reply.RoundtripTime.ToString() + " " + "ms";
                    panelUG[x].BackColor = Color.FromName("Lime");
                }
                else 
                {
                    lblSpdUG[x].Text = "Nonaktif";
                    panelUG[x].BackColor = Color.FromName("ButtonHighlight");
                }

            }
        }

Without a good, minimal , complete code example , it's hard to know for sure how to best answer your question. But it looks like you are trying to ping eight different servers, which are represented by eight set of controls in your form.

If that is correct, then I agree with commenter Hans Passant that you should be using the SendPingAsync() method instead. This will allow you to execute the pings asynchronously, without blocking the UI thread, so that your program can remain responsive.

Because you are dealing with eight different servers, it makes sense to me that you should execute the eight pings asynchronously. To accomplish this, I would refactor the code a bit, putting the server-specific loop body into a separate method, so that each instance can be run concurrently.

Implementing it that way would look something like this:

private async void CheckPOSUG(object sender, EventArgs e) 
{
    Panel[] panelUG = new Panel[]{pnlPOSUG1,pnlPOSUG2,pnlPOSUG3,pnlPOSUG4,pnlPOSUG5,pnlPOSUG6,pnlPOSUG7,pnlPOSUG8};
    Label[] LabelUG = new Label[]{lblUG1,lblUG2,lblUG3,lblUG4,lblUG5,lblUG6,lblUG7,lblUG8};
    Label[] lblSpdUG = new Label[] { lblSpdUG1, lblSpdUG2, lblSpdUG3, lblSpdUG4, lblSpdUG5, lblSpdUG6, lblSpdUG7, lblSpdUG8 };

    Task[] tasks = new Task[8];

    for (int x = 0; x < 8; x++)
    {
        tasks[x] = PingServer(x, panelUG[x], LabelUG[x], lblSpdUG[x]);
    }

    try
    {
        await Task.WhenAll(tasks);
    }
    catch (Exception e)
    {
        // handle as appropriate, e.g. log and exit program,
        // report expected, non-fatal exceptions, etc.
    }
}

async Task PingServer(int index, Panel panel, Label ugLabel, Label spdLabel)
{
    // NOTE: String concatenation will automatically convert
    // non-string operands by calling calling ToString()
    string IP = "192.168.135.1" + (index + 1);
    var ping = new Ping();
    var reply = await ping.SendPingAsync(IP, 10 * 1000);
    ugLabel.Text = "POSBMS10" + x;

    if (reply.Status == IPStatus.Success)
    {
        spdLabel.Text = reply.RoundtripTime + " ms";
        // The Color struct already has named properties for known colors,
        // so no need to pass a string to look Lime up.
        panel.BackColor = Color.Lime;
    }
    else 
    {
        spdLabel.Text = "Nonaktif";
        panel.BackColor = Color.FromName("ButtonHighlight");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM