3

I have created a windows service using c#, have a requirement of adding a timer during which service will be in idle state, and should start processing only after this idle timeout happens.

If device goes to sleep, will the timer start all over again or will it continue?

Luke Peterson
  • 8,584
  • 8
  • 45
  • 46
  • its a windows PC which is cofigured to go to sleep after 10 mins – shailesh91082 Jan 26 '15 at 08:05
  • possible duplicate of [What happens to timer in standby mode?](http://stackoverflow.com/questions/14821745/what-happens-to-timer-in-standby-mode) – Baldrick Jan 26 '15 at 08:23
  • 1
    What happened when you tried it? You did do some research first, before posting the question, right? Please share the details. – Peter Duniho Jan 26 '15 at 09:36
  • Yes i tried the way it is being mentioned in http://stackoverflow.com/questions/14821745/what-happens-to-timer-in-standby-mode?answertab=active#tab-top but i do not get the desired results. for Eg: Timer should fire after the time elapsed has arrived say in example is 60 secs, PC slept for 2 minutes after waking up immediately it should fire but it doesn't. Apart from this let's say PC slep t for 30 secs and then wake up, it should fire after next 30 secs but timer fires after totoal of 90 secs. – shailesh91082 Jan 26 '15 at 11:24

1 Answers1

0

Did Little bit of Gymnastics and identified a solution which i feel is good workable solution

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int maxtimeout = 100; // max timeout is 100 seconds, when it reaches this break

            var prevtime = ((DateTime.Now - DateTime.MinValue).TotalMilliseconds) / 1000;

            Console.WriteLine("current time = {0} seconds", prevtime);

            for (; ; )
            {

                Thread.Sleep(5000);
                var newtime = ((DateTime.Now - DateTime.MinValue).TotalMilliseconds) / 1000;
                var timeelapsed = newtime - prevtime;
                Console.WriteLine("Time elapsed = {0}", timeelapsed);
                if (timeelapsed >= maxtimeout)
                {
                    Console.WriteLine("Max timeout reached");
                    break;
                }
                else
                {
                    Console.WriteLine("Max timeout no reached elapased time = {0}", timeelapsed);
                }

            }
        }
    }
}