Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does nulling a System.Threading.Timer stop it?

Tags:

c#

If I have an active System.Threading.Timer and I set it to null, is it stopped?

I realize that it is more proper to call .Dispose() but I would like an answer to the question as written.

public class Foo
{
   private System.Threading.Timer _timer;
   public Foo()
   {
      // initialize timer
   }

   public void KillTimer()
   {
      _timer=null;
   }
}

Update:

After much back and forth about whether setting a single reference to a System.Threading.Timer to null will indeed result stopping it have shown that

  1. there are no lingering references, e.g. events list, as a threading timer takes a sinlge callback and does not expose events.
  2. that if GC collects, the finalizer will indeed dispose the TimerBase and stop the timer.

spike

using System;
using System.Threading;

namespace SO_3597276
{
    class Program
    {
        private static System.Threading.Timer _timer;

        static void Main(string[] args)
        {
            _timer = new Timer((s) => Console.WriteLine("fired"), null, 1000, Timeout.Infinite);
            _timer = null;
            GC.Collect();
            Console.ReadKey();
        }
    }
}

The timer callback is not called. Remove GC.Collect() and the callback is called.

Thanks all.

like image 345
Sky Sanders Avatar asked Aug 30 '10 01:08

Sky Sanders


People also ask

How do you stop a timer thread?

start() is a function that is used to initialize a timer. To end or quit the timer, one must use a cancel() function. Importing the threading class is necessary for one to use the threading class. The calling thread can be suspended for seconds using the function time.

Does system timers timer run in a separate thread?

Timer raises the elapsed event, is it raised in an independent thread? Yes, they run in a different thread.

What is System threading timer?

System.Threading. Timer Class. The Timer class (in the System. Threading namespace) is effective to periodically run a task on a separate thread. It provides a way to execute methods at specified intervals.

Is system threading timer thread safe?

Timer is not thread-safe.


1 Answers

Why would it?

Consider:

  System.Threading.Timer t = ...;
  System.Threading.Timer q = t;
  q = null; // Should this stop it as well?

Setting to null is something you do to a variable, not something you do to an object. The Timer has no way of knowing you set a particular variable to null, and cannot take action on the basis of that.

EDIT:

To address the edit, even in the case of having a sole reference, it is not guaranteed that the Timer will be stopped, as it is possible the GC may not run after the reference has been set to null. This is not entirely unlikely either, the Microsoft .NET implementation uses a generational collector and a static field will likely survive a nursery collection and be promoted to an older generation. If your program has a relatively stable memory profile there may never be a collection of the older generations (and by extension the finalizer will not run until the end of the program).

like image 119
Logan Capaldo Avatar answered Oct 14 '22 15:10

Logan Capaldo