Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to understand what <code>System.Timers.Timer</code> is doing behind the scenes. It is a wrapper around the <code>System.Threading.Timer</code> class. When the timer starts it creates a new <code>System.Threading.Timer</code> instance and passes it a callback method in the <code>System.Timers.Timer</code> instance. The system references this callback delegate as long as the timer remains enabled.</p> <p>We also know that delegates keep a reference to an instance of the class containing the target method. This is why your <code>System.Timers.Timer</code> instance is not collected and does not stop automatically. Removing the <code>Elapsed</code> event handler will not solve this problem because <em>that</em> delegate only holds a reference to the <code>TrayForm</code> instance. Again, it is the <code>System.Threading.Timer</code> that is keeping a reference to the <code>System.Timers.Timer</code>. The whole reference chain remains rooted because <em>the system</em> has to reference the <code>System.Threading.Timer</code> for it to work.</p> <p>Here is the reference chain:</p> <pre><code>System =&gt; _TimerCallback =&gt; Callback =&gt; System.Threading.Timer =&gt; Callback =&gt; System.Timers.Timer </code></pre> <p>When you track this through with Reflector or ILSpy you can see that the "System" in the reference chain above comes into play via the <code>TimerBase.AddTimerNative</code> method which is marked <code>MethodImplOptions.InternalCall</code> so we cannot see exactly how the reference is rooted below this point. But, it is rooted nonetheless.</p> <p>If you disable the timer via <code>Enabled = false</code> or <code>Stop</code> then it will dispose the underlying <code>System.Threading.Timer</code> which should in turn stop referencing the <code>System.Timers.Timer</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload