9

I have a COM add-in for Office apps that performs an operation in the background every 30 minutes using a

System.Timers.Timer

This background operation performs user authentication over an Internet connection, and I have reports from some users that authentication fails when their PCs wake from sleep. I cannot reproduce this behavior myself, so my theory is that when the PC wakes, the background operation executes before a connection to the Internet is restored (presumably, the connection was closed by Windows when the PC went to sleep).

In general, if a PC goes to sleep after a period of inactivity, what happens when the PC wakes (in terms of Timers)? For example, is the Timer paused while sleeping, and then resumes at its set interval when the PC wakes (e.g if the Timer was 10 min into the interval when the PC went to sleep, the Timer.Elapsed event won't fire until 20 min after the PC wakes)? Is the Timer.Elapsed event queued so that it fires immediately upon the PC waking? Does Windows stop the Timer when the PC goes to sleep? Or, does the Timer keep running while the PC sleeps, but since the Internet connection is broken (presumably), the background operation cannot perform user authentication?

OfficeAddinDev
  • 1,105
  • 3
  • 15
  • 29
  • You can subscribe to Windows power events, if that is of any use: [Get Sleep/Hibernate and Resume/Wakeup events in Visual Basic.NET](http://stackoverflow.com/a/11598083/1115360). – Andrew Morton Aug 06 '15 at 12:09
  • Would the same problem happen if the user lost his connection for a minutes right before the timer execute? – the_lotus Aug 06 '15 at 12:21
  • 1
    Your app needs to tolerate temporary connection loss anyway in all likelihood. You need something to deal with that such as retry. – usr Aug 06 '15 at 12:28
  • Answer was already given in the following question: http://stackoverflow.com/questions/14821745/what-happens-to-timer-in-standby-mode – deathx91 Oct 09 '15 at 19:40

1 Answers1

-2
    static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine(String.Format("{0}:{1}:{2} : Timer fired",
            DateTime.Now.ToLocalTime().Hour,
            "Execute:" + DateTime.Now.ToLocalTime().Minute,
            DateTime.Now.ToLocalTime().Second));
    }

when fire this event you will be know start time and whenever you want execute time just store where you manage execute time. if your pc goes sleep or hibernate and start again your pc before execute timing then you can check from your last execute time and can action perform.

M4N
  • 94,805
  • 45
  • 217
  • 260