Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:36:53+00:00 2026-05-27T01:36:53+00:00

Possible Duplicate: Timer, event and garbage collection : am I missing something? private void

  • 0

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

private void TrayForm_Load(object sender, EventArgs e)
{
    System.Timers.Timer HideMeTimer = new System.Timers.Timer(2500);
    HideMeTimer.Elapsed +=  
        delegate(object sender2, System.Timers.ElapsedEventArgs e2)
        {
            this.Invoke(new Action(somestuff));
        };
        HideMeTimer.Start();
        GC.Collect();
}

Can somebody please show me how the compilator will translate this?
Or good explanation why the timer keep on ticking after the load event.

(Is not the rule when the last reference is gone, to an variable, the GC will sometime
kill the object !)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:36:54+00:00Added an answer on May 27, 2026 at 1:36 am

    You need to understand what System.Timers.Timer is doing behind the scenes. It is a wrapper around the System.Threading.Timer class. When the timer starts it creates a new System.Threading.Timer instance and passes it a callback method in the System.Timers.Timer instance. The system references this callback delegate as long as the timer remains enabled.

    We also know that delegates keep a reference to an instance of the class containing the target method. This is why your System.Timers.Timer instance is not collected and does not stop automatically. Removing the Elapsed event handler will not solve this problem because that delegate only holds a reference to the TrayForm instance. Again, it is the System.Threading.Timer that is keeping a reference to the System.Timers.Timer. The whole reference chain remains rooted because the system has to reference the System.Threading.Timer for it to work.

    Here is the reference chain:

    System => _TimerCallback => Callback => System.Threading.Timer => Callback => System.Timers.Timer
    

    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 TimerBase.AddTimerNative method which is marked MethodImplOptions.InternalCall so we cannot see exactly how the reference is rooted below this point. But, it is rooted nonetheless.

    If you disable the timer via Enabled = false or Stop then it will dispose the underlying System.Threading.Timer which should in turn stop referencing the System.Timers.Timer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: Timer, event and garbage collection : am I missing something ? If
Possible Duplicate: System.Timer elapsed event seems to fire late for short intervals in .Net
Possible Duplicate: How to update GUI from another thread in C#? My Timer event
Possible Duplicate: C++ Timer function to provide time in nano seconds High resolution timer
Possible Duplicates: In a C# event handler, why must the "sender" parameter be an
Possible Duplicate: how to hide a div after some time period? i need to
Possible Duplicate: How can I specify the latest time of day with DateTime I
Possible Duplicate: Calling Python from Objective-C I'm a long-time Python programmer and short-time Cocoa
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.