2

Possible Duplicates:
Timer, event and garbage collection : am I missing something ?

If I'd have the following code:

public void SomeMethod()
{
    Timer timer = new Timer();
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}

will timer_Tick continue to be repeatedly called after the SomeMethod has finished even though there is no reference to the timer anywhere?

I'm thinking maybe it will until the Timer's Dispose method is called. But then, in general, does the GC knows when an object have not been disposed?

Community
  • 1
  • 1
Juan
  • 15,274
  • 23
  • 105
  • 187
  • Only `System.Windows.Forms.Timer` or `System.Web.UI` have a `Tick`. – Stephen Cleary Jul 08 '11 at 20:30
  • Does it matter? `System.Windows.Form.Timer`. But I'm just using it as a sample. I want to know in general how this works. – Juan Jul 08 '11 at 20:30
  • I believe it is System.Windows.Forms.Timer as this is the only one with an event named Tick. (afaik) – luketorjussen Jul 08 '11 at 20:31
  • GC knows about all managed objects. – Tomas Voracek Jul 08 '11 at 20:31
  • @Henk But this question is not a duplicate of the proposed one. The proposed one is about DispatcherTimer, this question here doesn't use that. I too believe that this question has been asked before, but we should at least find a *correct* duplicate before it is closed as such. – Lasse V. Karlsen Jul 08 '11 at 20:36
  • @Lasse: The OP said it didn't matter which timer... And as long as there's no specific Timer here you can't find an exact dupe. I suggest NARQ. – H H Jul 08 '11 at 20:40
  • 1
    In general, I agree, because all the timers behave differently, so there is no way to answer *that* question. Still, since he said one of them specifically in a comment, I closed as a duplicate. – Lasse V. Karlsen Jul 08 '11 at 20:41
  • 1
    @jsoldi Please don't reask in general about all the timers, they all behave differently, so if you do ask that question, it will be closed as not-a-real-question, as @Henk says. Since you specifically mentioned the System.Windows.Forms one, I closed your question as a duplicate of one answering for that timer. There are other questions here with answers for the other timers as well, at least DispatcherTimer has one. – Lasse V. Karlsen Jul 08 '11 at 20:43

3 Answers3

2

Contrary to the other answers here, no, the timer will not be garbage collected.

Internally, enabling the timer will allocate a GCHandle object, which will keep the object, and thus the timer, and thus whatever object has the implementing event handler(s) alive until you disable it, or the program ends, whichever comes first.

This has already been answered here on SO, here: Timer, event and garbage collection : am I missing something ?, so I'm going to close this question as a duplicate.

Community
  • 1
  • 1
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
1

No code anywhere will be holding a reference to timer; therefore it will be eligible for garbage collection.

The timer object itself has a reference to your timer_Tick method, but that doesn't matter. It can still be collected.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • Which won't necessarily just stop the timer but possibly cause an exception depending on how the `Timer` is designed. This kinda sucks about managed garbage collection. – Juan Jul 08 '11 at 20:40
0

The GC will collect it at his next crawl... It is like any other object. The timer is referencing to the event, not the event to the instance, so it will be GC'd.

Aykut Çevik
  • 2,060
  • 3
  • 20
  • 27