开发者

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()
    开发者_Python百科{



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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜