1

I have a Window-Form Application connecting to a server for database updates. When application starts, I connect to the internet. On the Form I show a green Button, which shows internet connectivity on Load-Event. If there is no connectivity then it shows a red button.

public bool check()
{
    try
    {
        Ping myPing = new Ping();
        String host = "google.com";
        byte[] buffer = new byte[32];
        int timeout = 1000;
        PingOptions pingOptions = new PingOptions();
        PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
        return (reply.Status == IPStatus.Success);
    }
    catch (Exception)
    {
       return false;
    }
}

I want this button change it's color automatic, when ever it lost it's connectivity from internet or when internet come back.

How i can implement this?

Sebi
  • 3,879
  • 2
  • 35
  • 62
Seeker
  • 15
  • 1
  • 7
  • Use a timer to call your `Check` method every X milliseconds – Pikoh Jan 30 '17 at 12:11
  • You can use a ' Timer'. And `on timer.Tick` you can insert the code to update the button color. – Wudge Jan 30 '17 at 12:12
  • I can do that but is there inbuilt class or any another method to perform this action automatic rather then timer keep running – Seeker Jan 30 '17 at 12:15
  • I'd suggest that you implement a task to check out the connection availability. – M. A. Cordeiro Jan 30 '17 at 12:39
  • static void Main(string[] args) { NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged); Console.WriteLine("Its main"); Console.ReadLine(); } static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e) { if (e.IsAvailable) { Console.WriteLine("its event"); } } } I have tried this network change class but does not work – Seeker Jan 31 '17 at 07:46

0 Answers0