Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop System.Threading.Timer in callback method

How can I stop System.Threading.Timer in it's call back method. I referenced MSDN, but couldn't find anything useful. Please help.

like image 612
Proceso Avatar asked Aug 13 '12 16:08

Proceso


3 Answers

First, the callback method must have the timer instance in-scope.

Then the simple incantation

timerInstance.Change( Timeout.Infinite , Timeout.Infinite ) ;

will shut down the timer. It is possible that the timer might invoke the callback method once more after the change, I believe, depending on the state it's in.

like image 103
Nicholas Carey Avatar answered Oct 13 '22 20:10

Nicholas Carey


The problem with Timer is that it might be called after disposing its owner class. The following implementation worked for me by using the state object of the Timer initializer. Heap will not remove that object until it is consumed. This was my only way to gracefully cleanup timer callback.

using System;
using System.Threading;

namespace TimerDispose
{
    /// <summary>
    /// A timer-containing class that can be disposed safely by allowing the timer 
    /// callback that it must exit/cancel its processes
    /// </summary>
    class TimerOwner : IDisposable
    {
        const int dueTime = 5 * 100;       //halve a second
        const int timerPeriod = 1 * 1000;   //Repeat timer every one second (make it Timeout.Inifinite if no repeating required)

        private TimerCanceller timerCanceller = new TimerCanceller();

        private Timer timer;

        public TimerOwner()
        {
            timerInit(dueTime);
        }

        byte[] dummy = new byte[100000];

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dueTime">Pass dueTime for the first time, then TimerPeriod will be passed automatically</param>
        private void timerInit(int dueTime)
        {

            timer = new Timer(timerCallback,
                timerCanceller,     //this is the trick, it will be kept in the heap until it is consumed by the callback 
                dueTime,
                Timeout.Infinite
            );

        }

        private void timerCallback(object state)
        {
            try
            {
                //First exit if the timer was stoped before calling callback. This info is saved in state
                var canceller = (TimerCanceller)state;
                if (canceller.Cancelled)
                {
                    return; //
                }

                //Your logic goes here. Please take care ! the callback might have already been called before stoping the timer
                //and we might be already here after intending of stoping the timer. In most cases it is fine but try not to consume
                //an object of this class because it might be already disposed. If you have to do that, hopefully it will be catched by
                //the ObjectDisposedException below




                dummy[1] = 50;  //just messing up with the object after it might be disposed/nulled

                //Yes, we need to check again. Read above note
                if (canceller.Cancelled)
                {
                    //Dispose any resource that might have been initialized above
                    return; //
                }

                if (timerPeriod != Timeout.Infinite)
                {
                    timerInit(timerPeriod);
                }
            }
            catch (ObjectDisposedException ex)
            {
                Console.WriteLine("A disposed object accessed");
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine("A nulled object accessed");
            }
            catch (Exception ex)
            {

            }
        }

        public void releaseTimer()
        {
            timerCanceller.Cancelled = true;
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            timer.Dispose();
        }

        public void Dispose()
        {
            releaseTimer();
            dummy = null;   //for testing
            GC.SuppressFinalize(this);
        }
    }

    class TimerCanceller
    {
        public bool Cancelled = false;
    }


    /// <summary>
    /// Testing the implementation
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            var list = new System.Collections.Generic.List<TimerOwner>();
            Console.WriteLine("Started initializing");
            for (int i = 0; i < 500000; i++)
            {
                list.Add(new TimerOwner());
            }

            Console.WriteLine("Started releasing");
            foreach (var item in list)
            {
                item.Dispose();
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}
like image 41
Ibrahim Avatar answered Oct 13 '22 18:10

Ibrahim


I found out the hard way that Change(Timeout.Infinite, Timeout.Infinite) isn't quite reliable, and switched over to System.Timers.Timer with AutoReset = false.

like image 35
Mark Sowul Avatar answered Oct 13 '22 20:10

Mark Sowul