views:

747

answers:

5

I have created a timeclock application in C# that connects to a web service on our server in order to clock employees in/out. The application resides in the system tray and clocks users out if they shut down/suspend their machines or if they are idle for more than three hours to which it clocks them out at the time of last activity.

My issue arises that when a user brings his machine back up from a sleep state (which fires the SystemEvents.PowerModeChanged event), the application attempts to clock the employee back in but the network connection isn't fully initialized at that time and the web-service call times out.

An obvious solution, albeit it a hack, would be to put a delay on the clock in but this wouldn't necessarily fix the problem across the board. What I am looking to do is a sort of "relentless" clock in where it will wait until it can see the server until it actually attempts to clock in.

What is the best method to determine if a connection to a web service can be made?

+3  A: 

The best way is going to be to actually try to make the connection and catch the errors. You can ping the machine, but that will only tell you if the machine is running and on the network, which doesn't necessarily reflect on whether the webservice is running and available.

Scott Dorman
A ping wont even necessarily tell you that much.
Max Schmeling
True. If the server normally doesn't respond to ping requests, it would be useless.
Scott Dorman
A: 

When handling the event, put your connection code into a method that will loop through until success, catching errors and retrying.

Even a delay wouldn't be perfect as depending on the individual systems and other applications running it can take varying times for the network connection to be re-established.

Mitchel Sellers
A: 

if the problem is latency in re-establishing the network service, Ping is the solution; it's like ringing the doorbell to see if anyone is home

if ping succeeds, then try calling the web service, catching exceptions appropriately (I think both SocketException and SoapException can occur depending on readiness/responsiveness)

Steven A. Lowe
A: 

Implement a queue where you post messages and have a thread periodically try to flush the in-memory queue to the web service.

Andrei Rinea
A: 

Ping can be disabled although the web service port is open. I wouldn't use this method...