….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public bool Ping() { Ping p = new Ping(); try { PingReply reply = p.Send("www.kutayzorlu.com", 1000); if (reply.Status == IPStatus.Success) return true; } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } return false; } private void start_Click(object sender, EventArgs e) { for (; ; ) // unl[/csharp]ess { Console.WriteLine(Ping); } } |
Creating a Timer, for this method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class NetworkStateMonitor { private System.Threading.Timer _timer; bool _wasConnected = false; public NetworkStateMonitor() { _timer = new System.Threading.Timer(OnPing, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); } public bool CheckInternetConnection() { bool result = false; Ping p = new Ping(); try { PingReply reply = p.Send("www.kutayzorlu.com", 1000); if (reply.Status == IPStatus.Success) return true; catch (PingException) { return false; } } private void OnPing(object state) { var newState = CheckInternetConnection(); if (!newState && _wasConnected) ConnectionLost(this, EventArgs.Empty); else if (newState && !_wasConnected) Connected(this, EventArgs.Empty); _wasConnected = newState; } public event EventHandler ConnectionLost = delegate{}; ******* public event EventHandler Connected = delegate{}; ****** } |