当系统没有互联网连接时,在应用程序顶部显示一条消息 [英] Showing a message on top of application when system has no internet connectivity

查看:74
本文介绍了当系统没有互联网连接时,在应用程序顶部显示一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我创建了使用远程在线MYSQL数据库的Windows应用程序.

我的要求是,每当系统没有Internet连接时,都会出现一条弹出消息,直到重新建立连接为止,用户只能关闭应用程序才能执行任何操作.

我正在上这样的课..

Hello there,
I''ve created windows application which uses an remote online MYSQL database.

My requirement is, whenever system has no internet connectivity, a pop up message should come and till the connection re established, the user won''t be able to do anything except closing the application..

I''m having a class like this..

public bool ConnectionExists()
        {
            try
            {
                System.Net.Sockets.TcpClient clnt = new System.Net.Sockets.TcpClient("www.google.com", 80);
                clnt.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }



我是否应该将其用作将在整个应用程序中运行的线程?或有什么主意吗?

谢谢..
Debasish



Should I use this as a thread which will run through out the application? or any idea?

Thanx..
Debasish

推荐答案

我不会打扰线程.如果连接尝试失败,您可以简单地告诉用户与数据库的连接不可用,然后稍后重新启动应用程序.然后,您可以自己关闭该应用程序,也可以让用户关闭它,然后稍后重新启动它.互联网是否可用并不是定义关闭应用程序的真正问题,而是缺少数据库连接.
I wouldn''t bother with a thread. If the connection attempt fails, you can simply tell the user that the connection to the database is not available and to restart the application later. Then you can either close the app yourself or let the user close it and then restart it later. Whether or not internet is available isn''t really the issue defining your choice to close the app, it is the lack of db connection.


NetworkInterface <时很有用/a> [^ ]- NetworkInterface.GetIsNetworkAvailable [ ^ ]来检测是否有可用的网络.

如果不是这样,向用户显示您能够检测到的有关计算机配置的信息通常会很有用,因为这可能会使您的支持电话更容易处理.

此Google搜索^ ]提供了许多链接,解释了为什么需要使用 Control.Invoke [ ^ ],当您想与gui线程进行交互时,如SAKryukov试图在他对先前问题的回答中进行解释.

问候
Espen Harlinn
How To: (Almost) Everything In WMI via C# - Part 3: Hardware[^] allows you to detect whether your computer is connected to a network - remember the computer may be connected to a network without being able to access the internet.

The classes
Win32_NetworkAdapter
Win32_NetworkAdapterConfiguration

provides you with enough information to detect network configuration - useful when you try to use NetworkInterface[^] - NetworkInterface.GetIsNetworkAvailable[^] to detect if there is an available network.

When it isn''t it''s quite often more useful to display the information you are able to detect about the computers configuration to the user, as it will probably make your support calls a lot easier to handle.

This google search invokerequired/invoke pattern[^] provides a number of links explaining why you need to use Control.Invoke[^] when you want to interact with the gui thread, as SAKryukov tried to explain in his answer to your previous question.

Regards
Espen Harlinn


这篇关于当系统没有互联网连接时,在应用程序顶部显示一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆