1

I write a simple program that sends ICMP-requests. The program polls every host in own thread. The problem is flickering table on Form with labels and buttons where are shown results. I try set on Doublebuffered in Form, but it don't help.

private void pingThreadmain(object number_thread) {            
    Ping pingSender = new Ping();            
    // поток работает, пока установлена глобальная переменная
    while (thread_ping) {                
        // замеряем время запуска пинга
        int start_ping = Environment.TickCount & Int32.MaxValue;
        // получаем таймаут из таблицы
        string timeout = "";
        Action action_timeout = () => timeout = list_timeout[(int)number_thread].Text;
        list_timeout[(int)number_thread].Invoke(action_timeout);
        // пингуем хост
        PingReply reply = pingSender.Send(list_ip[(int)number_thread].Text, Int32.Parse(timeout));
        // вычисляем время выполнения пинга
        int duration_ping = Int32.Parse(timeout) - (Environment.TickCount & Int32.MaxValue) + start_ping;
        if (reply.Status == IPStatus.Success) {
            // если хост отвечает
            // изменяем визуальные компоненты
            Action action_status = () => list_status[(int)number_thread].BackColor = Color.Green;
            list_status[(int)number_thread].Invoke(action_status);
            Action action_rtt = () => list_rtt[(int)number_thread].Text = reply.RoundtripTime.ToString();
            list_rtt[(int)number_thread].Invoke(action_rtt);
            // проверяем что таймаут паузы не отрицательный
            if (duration_ping > 0) {
                Thread.Sleep(duration_ping);
            }
        }
        else {
            // если хост не отвечает
            // изменяем визуальные компоненты 
            Action action_status = () => list_status[(int)number_thread].BackColor = Color.Red;
            list_status[(int)number_thread].Invoke(action_status);
            Action action_rtt = () => list_rtt[(int)number_thread].Text = "---";
            list_rtt[(int)number_thread].Invoke(action_rtt);
            Action action_fail = () => list_fail[(int)number_thread].Text = (Int32.Parse(list_fail[(int)number_thread].Text) + 1).ToString();
            list_fail[(int)number_thread].Invoke(action_fail);
            // выводим сообщение о недоступности хоста
            AddLog($"нет ответа от {list_ip[(int)number_thread].Text} {list_name[(int)number_thread].Text}");
            // проверяем что таймаут паузы не отрицательный
            if (duration_ping > 0) {
                Thread.Sleep(duration_ping);
            }
        }
    }
}
IndieGameDev
  • 2,905
  • 3
  • 16
  • 29
rugo
  • 11
  • 1

1 Answers1

0

The line this.DoubleBuffered = true should have worked.

It seems that it is not in the YourProject.YourProject class

It should be in the class so that the double buffering can be correctly implemented.

You should also try ControlStyles Enum ControlStyles . It should be implemented under SetStyle, which must be of the type YourProject.YourProject.

You can find additional ways here - how to stop flickering C# winforms.

LopDev
  • 823
  • 10
  • 26
bimbi
  • 70
  • 10