Timer is used to monitor whether the network is pinged and timer is pinged.
The project needs to connect to a specific server. If the server cannot be connected, the system prompts in real time to start using Timer for real-time detection.
Void timer_Tick (object sender, EventArgs e) {Ping pingSender = new Ping (); PingReply reply = pingSender. Send ("192.168.1.104", 1000); if (reply. Status! = IPStatus. Success) {Message. show ("network exception ")}}
If I run Timer all the time, the memory of the program will keep increasing. Later, the test found that it is not a problem with Timer. Timer has been running without affecting the memory. It is caused by Ping in Timer, it is estimated that there will be a return value after Ping, not cleared
After modification, Ping pingSender = new Ping () to add a PingCompleted event and release it to solve the problem.
Void pingSender_PingCompleted (object sender, PingCompletedEventArgs e) {(sender as IDisposable). Dispose ();}
Follow-up questions:
If a timeout occurs during the ping operation, an error occurs. Make a modification. If the number of ping failures exceeds the specified number of times, an error is returned.
The second problem is whether the computer bandwidth of the IP address will be blocked when thousands of computers are pinging an IP address.
Thank you!