1

I have a Timer in a C# app (Win7 64). Every 20 min the timer callback gets some data via HTTP. It works well. But if my PC goes to sleep for a few hours, then wakes up, I get an HTTP error saying "can't find the server". The server is running at all times, so it's not the real issue. I suspect the real reason is that as the timer callback misses its "time slot" during the sleep period, it becomes "overdue" and is executed instantly right after the wake-up, without waiting for the PC to re-establish the Internet connection. I can fix the problem by adding a Thread.Sleep(5000) at the beginning of the callback, but it's a bit of a kludge (establishing Internet connection might take longer). My questions:

  1. After a wake-up from sleep mode, does Win indeed need a few seconds to re-establish Internet connection? My connection is DSL, "always online".

  2. Is there a C# system call which would wait until Win establishes the Internet connection (or time out if this doesn't happen within, say, 20 secs)?

EDIT: Just found this: Check Net connection in C#. Not as simple as I hoped, it takes a sequence of checks: any LAN connection, DNS lookup (outside the LAN), ping a server (check a particular server).

Community
  • 1
  • 1
MrSparkly
  • 627
  • 1
  • 7
  • 17
  • If you can't find the server, just try again after a short timeout? – spender Nov 27 '11 at 05:00
  • I was hoping I could tell the difference between 1) the server's really down (user gets an error message), and 2) there's no net connection (right after system wakeup): don't even bother checking the server until net connection exists (and don't bother the user). – MrSparkly Nov 27 '11 at 05:19

2 Answers2

2

1) Yes 2) You may check various connection attributes via WMI for example. See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394595%28v=vs.85%29.aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216%28v=vs.85%29.aspx to make sure you have a connection before attempting a query.

Null
  • 93
  • 7
1

I don't think you should bother with WMI and such.

If you cannot access the network, set the timer to fire in a minuts instead of 20. You alrsady have a waiting mechanism in place, reusing it will not seem kludgy.

zmbq
  • 38,013
  • 14
  • 101
  • 171