hmlasnk
hmlasnk

Reputation: 1322

WPF startup application execute after internet connection connected?

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??????

Upvotes: 1

Views: 1349

Answers (1)

Dean Kuga
Dean Kuga

Reputation: 12119

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.

Upvotes: 3

Related Questions