2

I am developing a project that uses SQL Server Express to house data on a laptop and sync back to a network server once connected to the network.

Is there a way to sense a network connection and then trigger an event? I know I could do this with a timer that checks for network connections periodically, but I would like for the event to run as soon as the device establishes a connection.

Nate S.
  • 1,117
  • 15
  • 31

2 Answers2

7

You can register to NetworkChange.NetworkAvailabilityChanged event for receiving events when the availability of the network changes

From MSDN:

The NetworkChange class raises NetworkAvailabilityChanged events when the availability of the network changes. The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface.

To have a NetworkChange object call an event-handling method when a NetworkAvailabilityChanged event occurs, you must associate the method with a NetworkAvailabilityChangedEventHandler delegate, and add this delegate to this event.

The NetworkAvailabilityChanged event is supported on Windows 2000 and later.

For more information http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx

Community
  • 1
  • 1
yakiro
  • 718
  • 4
  • 9
2

Maybe you can try setting up a schedule to run your application, using windows task scheduler? I know that in windows 7 you can do that and you are able to set a running condition: Only when a certain network is available. I think it would solve you a lot of trouble. Other than that maybe you can try : I need a event to detect Internet connect/disconnect

and the first answer sends you too:

http://msdn.microsoft.com/en-us/library/ee264321(VS.85).aspx

Community
  • 1
  • 1
Vlad
  • 536
  • 1
  • 5
  • 18