1

I'm developing a windows application using WPF.The program is running in the startup and should wait untill the internet connection is connected, to be executed. Normally internet connection will get some time to connect. Therefore now im running a thread to ping(like this) with the server in space of 3 seconds to watch the connection status.

 public bool CheckConnection()
    {



        try
        {

            //////////////check connction
            System.Net.Sockets.TcpClient clnt = new System.Net.Sockets.TcpClient(UserConfig.configlist[2], Convert.ToInt32(UserConfig.configlist[3]));
            clnt.Close();
            return true;

        }
        catch (Exception)
        {

            return false;

        }
    }

If the status is true program will be executed. Is there any efficient way of doing this. Any ideas please??????

Tom Studee
  • 10,316
  • 4
  • 38
  • 42
hmlasnk
  • 1,160
  • 1
  • 14
  • 33
  • 1
    You might find this question useful http://stackoverflow.com/questions/843810/c-fastest-way-to-test-internet-connection – Tom Studee Jul 21 '11 at 04:15

1 Answers1

3

There is a very useful NetworkManager class over on CP that will allow you to check for network connection status using the NetConnectionStatus enum...

You could start a timer to check for network connection every couple of seconds with if (NetworkManager.NetConnectionStatus != NetConnectionStatus.Connected){} and wait to execute your network dependent code until the network status changes to Connected.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • Thanks i'm a new comer.I didn't know that tick was requited.thanks again for your answer. – hmlasnk Jul 21 '11 at 17:09
  • No problem, welcome, it is not required but that's how the system works... if someone gives you a good answer you give them thumbs up... – Dean Kuga Jul 23 '11 at 15:07