How can I make an indicator of resource availability? Ie, there is a resource that needs to be checked whether it is available or not; constantly pinging is not an option at the moment:

label.Content = new Ping().Send("172.30.216.27").RoundtripTime.ToString() + "ms"; 

There is an idea to make a green access indicator red no, poll every 20 seconds. But I do not know how to do this, please help.

UPD People help stop this flow

 new Thread(() => { var ipAddr = _label.Text; if (!string.IsNullOrEmpty(ipAddr)) { ls1: try { var pingSender = new Ping(); var reply = pingSender.Send(ipAddr); if (reply != null && reply.Status == IPStatus.Success) { Invoke((Action) (() => { _label.BackColor = Color.Green; })); } else { Invoke((Action) (() => { _label.BackColor = Color.Red; })); } } catch { this.Invoke((Action) (() => { _label.BackColor = Color.Red; })); } Thread.Sleep(20000); goto ls1; } }).Start(); 
  • When the connection is broken, start the timer and turn on the red light, ping in it, if successful, stop the timer and turn on the green light - Dmitry Chistik

2 answers 2

You can still do this:

  new System.Threading.Thread(() => { Ping pingSender = new Ping(); IPAddress address = IPAddress.Parse("172.30.216.27"); PingReply reply; ls1: try { reply = pingSender.Send(address); if (reply.Status == IPStatus.Success) { Dispatcher.BeginInvoke((Action)(() => { label.Background = Brushes.Green; label.Content = "Ping "+reply.RoundtripTime.ToString() + " ms"; })); } else { Dispatcher.BeginInvoke((Action)(() => { label.Background = Brushes.Red; label.Content = "No answer"; })); } } catch { Dispatcher.BeginInvoke((Action)(() => { label.Background = Brushes.Red; label.Content = "No answer"; })); } Thread.Sleep(20000); goto ls1; }).Start(); 

Just insert this code at the beginning of the program and once every 20 seconds it will send a ping to the resource. In the example, it changes the background color of label1. Accordingly, you can change the color of the element.

UPD . Made changes and added a handler because an error popped up when the connection was broken.

  • In principle, it is suitable if you do this if (reply.Status == IPStatus.Success) {label.Background = Brushes.Green; } else {label.Background = Brushes.Red; } here is the problem. error Additional Information: The caller cannot access this object, as the owner of this object is another thread. Swears on label.Background = Brushes.Green; - Pavel
  • This is because you need Invoke . You can write in the condition like this: this.Dispatcher.BeginInvoke((Action)(() => { label.Background = Brushes.Green; })); - Vadim Pavlovich
  • All a big thank you helped with a small refinement in response you can make all the changes: Dispatcher.BeginInvoke ((Action) (() => {label.Background = Brushes.Red;})); - Pavel
  • you can still use do {} while (true) instead of goto - Dmitry Chistik
  • People help stop this miracle, because you have to kill the process. - Pavel

A bit about DispatcherTimer :

 //Создаём System.Windows.Threading.DispatcherTimer m_PingTimer = new System.Windows.Threading.DispatcherTimer() { Interval = new TimeSpan(0, 0, 20) }; //Создаем делегат тика m_PingTimer.Tick += delegate(object sender, EventArgs e) { //Тут делаем Вашу магию с пингами label.Content = new Ping().Send("172.30.216.27").RoundtripTime.ToString() + "ms"; if (всё зашибись) m_PingTimer.Stop(); }; if (всё плохо) m_PingTimer.Start();