Microsoft Developer Network > Forums Home > Visual C# Forums > Visual C# Language > Notify instead of Monitor Internet Connection Status
Ask a questionAsk a question
 

AnswerNotify instead of Monitor Internet Connection Status

  • Thursday, November 05, 2009 11:01 AMAkeeq Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    Some one please help me on how to monitor internet connection status. wheather is available or not.
    I have searched on this didn't find 

    what i found is

    1. NetworkChange.NetworkAvailabilityChanged event

    but this will only detect the network availability. I want a notification similar to this for internet connection availability. If there is network and only internet is gone then this will fail.


    2. Using a function to detect internet connection status (got several such function)

    this will work fine. but i have to put this in a thread to check condinously. Is that is the only way or is there any other way to get notification or will call event handler function like the previous. I think their will be one.

    or some body can tell where the windows will make entry in the registry for internet connection status. I know to make notification for a specific key on registry. Which if changes will call the notify-function.

    Please help me on how the internet related program (like messengers) are notified at the instance the internet is disconnected

    Thanks in Advance
    Akeeq
    • Edited byAkeeq Thursday, November 05, 2009 11:31 AMmore undestood
    •  

Answers

All Replies

  • Thursday, November 05, 2009 11:09 AMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hi,

    When programs such as messenger could not establish connection to their server they go into disconnected state.

    You can do the similar.

    You can ping a server at specific time intervals

                System.Net.NetworkInformation.Ping png = new System.Net.NetworkInformation.Ping();
                 try
                 {
                     PingReply rply = png.Send("www.bing.com");
                     Console.Write(rply.RoundtripTime.ToString());
                     Console.ReadLine();
                 }
                 catch (Exception)
                 {
                     Console.Write("Ping Error");
                     Console.ReadLine();
                 }
    

     

    Note:Using ip address instead of using domain might be a better way to prevent errors related to DNS.
  • Thursday, November 05, 2009 11:26 AMAkeeq Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    Any one know how to create WMI internet connection notification function.

    i dont like to create thread or any timer which will consume CPU and memory

    but if that is the only way then please tell how it can impliment effectively.

    Thanks
  • Thursday, November 05, 2009 11:34 AMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    Hi,

    Did some research and found that you can get this information with an api.

    Here is a sample.

            [DllImport("wininet.dll")]
            private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
    
            public static bool IsInternetConnectionAvailable()
            {
                int d;
                return InternetGetConnectedState(out d, 0);
            }
    
            static void Main()
            {
                Console.WriteLine(IsInternetConnectionAvailable());
                Console.ReadKey();
            }
    
    • Proposed As Answer byTamer OzMVPFriday, November 06, 2009 12:11 PM
    •  
  • Saturday, November 07, 2009 11:52 AMAkeeq Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    I know my English is poor. But now i realized how much deep it is

    Please ...

    i know how to get connection status
    what i need is a call back or notification function when ever internet connection status changes

    If their is no such thing, please tell that  or how can i implement this


  • Saturday, November 07, 2009 11:58 AMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    • Marked As Answer byAkeeq Wednesday, November 11, 2009 10:51 AM
    •  
  • Monday, November 09, 2009 7:20 AMHarry ZhuMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    As far as I can see, this kind of event does not exist.
    We might need to query every time using timer.

    I believe messagers get notifications when they sent information but do not get response. They will not receive notification when the internet status is changed.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked As Answer byAkeeq Wednesday, November 11, 2009 10:50 AM
    •