none
System.Threading.Timer stuck from computer awake from sleep RRS feed

  • Question

  • Hi all,

     

    I am developing Dot Net based console application and using System.Threading.Timer to a perform task repeatedly. The program is running perfectly; the issue is that callback function called in Timer object stops getting called when PC awake from sleep mode, however application still in sunning mode. Please help me to get this issue resolved.

    Thanks

    Friday, March 31, 2017 7:23 AM

All replies

  • Hi Ali  Raza,

    According to your description, I'm not sure what cause the issue, could you please provide a bit more information, such as related code, which I could reproduce the issue on my side.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 3, 2017 1:02 AM
    Moderator
  • Btw, you can listen to the power event, then suspend and resume the timer(s) accordingly.

    Monday, April 3, 2017 3:44 AM
    Answerer
  • If I am getting you, your prob statement is the timer in your application is not working though your application is working when your pc awakes from sleep mode, right?

    If (yes)

      link can help

    else 

     elaborate your problem statement


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Thursday, April 6, 2017 11:01 AM
  • Without seeing what your application does its hard to say why timer does not resume from sleep. Usually threading timer is working even when in sleep if you do not explicitly suspend it.

    The following code shows console application that uses threading timer. There is two different behaviors controller by suspendTimerWhenSleeping field. When set to false, timer is not suspended when computer is sleeping. When set to false, timer is suspended when computer is sleeping. You see the behavior difference in images below.

    class Program
    {
        const int SignalPeriod = 1000;
        const int NoDueTime = 0;
    
        static Timer timer;
        static bool suspendTimerWhenSleeping;
        static bool sleeping;
    
        static void Main(string[] args)
        {
            Enter();
                
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
    
            Exit();
        }
    
        static void Enter()
        {
            SystemEvents.PowerModeChanged += OnPowerModeChanged;
            SystemEvents.SessionEnding += OnSessionEnding;
    
            sleeping = false;
            suspendTimerWhenSleeping = true;
    
            timer = new Timer(new TimerCallback(OnTimerCallback), null, NoDueTime, SignalPeriod);
        }
    
        static void Exit()
        {
            SystemEvents.PowerModeChanged -= OnPowerModeChanged;
            SystemEvents.SessionEnding -= OnSessionEnding;
        }
    
        static void Suspend()
        {
            if (suspendTimerWhenSleeping)
            {
                Console.WriteLine("Suspend timer.");
                timer.Change(Timeout.Infinite, Timeout.Infinite);
            }
    
            sleeping = true;
        }
    
        static void Resume()
        {
            if (suspendTimerWhenSleeping)
            {
                Console.WriteLine("Resume timer.");
                timer.Change(NoDueTime, SignalPeriod);
            }
    
            sleeping = false;
        }
    
        static void OnSessionEnding(object sender, SessionEndingEventArgs e)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
        }
    
        static void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
        {
            if (e.Mode == PowerModes.Suspend)
            {
                Suspend();
            }
            else if (e.Mode == PowerModes.Resume)
            {
                Resume();
            }
        }
    
        static void OnTimerCallback(object state)
        {
            Console.WriteLine("Timer invoked {0}, sleeping {1}.", DateTime.Now.ToString("G"), sleeping);
        }
    }

    Behavior when timer not suspended:

    Timer not suspended

    Behavior when timer is suspended:

    Timer suspended.

    So if you do not suspend timer it should be still work when computer is sleeping and resumes. Without seeing code it might be that you suspend timer when computer is set to sleeping, but do not resume it. Anyway if you want behavior where timer is suspended during sleep, then its best to handle power off event and disable timer during sleep like in code above when suspendTimerWhenSleeping is true.

    Friday, April 7, 2017 5:27 AM