0

I am creating a Win Forms Application(C#). I need to keep it running 24/7 and submit some XML data when connected to Internet.

I am aware of few methods which keep pinging a solid domain to check if the system is connected to Internet.

But I am looking for some better or elegant way to do the same. I have some hints that I may be able to capture some socket events. Please help me with some ideas.

typ1232
  • 5,535
  • 6
  • 35
  • 51
Jason Dias
  • 769
  • 1
  • 7
  • 9
  • 3
    Try to connect with your socket and send data on success, you can fire some event also, but it does not matter, just retry connection and if connected send. – Mateusz May 23 '13 at 20:59
  • 1
    Do you want to *respond* to *a change* in network availability? In that case perhaps [NetworkAvailabilityChanged](http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx) might be of interest. – Patrick May 23 '13 at 21:58

1 Answers1

1

See: How to test if Webbrowser gets a connection error when navigating to a new URL?

[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );

//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{
    int Desc ;
    return InternetGetConnectedState( out Desc, 0 ) ;
}
}

You can get more info about the function here.

Community
  • 1
  • 1
Gojira
  • 2,941
  • 2
  • 21
  • 30
  • Thanks but this worked for me :) NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged)‌​; – Jason Dias May 30 '13 at 18:46
  • That's handy too! For some apps, you might consider a combo approach - e.g., your app may not have been running when the network availability changed, necessitating an initial check before listening for additional events. Anyways, glad you found something that works. – Gojira Jun 04 '13 at 18:23