Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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..
C#
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;
            }
        }


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

Thanx..
Debasish
Posted
Updated 8-Mar-11 12:44pm
v2

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.
 
Share this answer
 
Comments
DEB4u 8-Mar-11 15:55pm    
Actually needs to show the internet status in apllication, always...
fjdiewornncalwe 10-Mar-11 6:58am    
I see. In that case, I would build a small singleton class that can spawn a thread that will check the connectivity every x number of seconds and send an event to the owning application which in turn updates a user control of some kind. In most cases I would have the user control that shows the status own the singleton object directly so that you can just drop an instance of the usercontrol on any form you like to get it displayed. I would use Espen's answer for how to check the connectivity itself. The user control can also spawn the "connection lost" message box.
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Mar-11 0:11am    
Very good set of information source, especially short WMI article, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900