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

  • SEARCH
  • Home
  • 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 714125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:02:24+00:00 2026-05-14T05:02:24+00:00

I just gave an answer to a quite simple question by using an extension

  • 0

I just gave an answer to a quite simple question by using an extension method. But after writing it down i remembered that you can’t unsubscribe a lambda from an event handler.

So far no big problem. But how does all this behave within an extension method??

Below is my code snipped again. So can anyone enlighten me, if this will lead to myriads of timers hanging around in memory if you call this extension method multiple times?

I would say no, cause the scope of the timer is limited within this function. So after leaving it no one else has a reference to this object. I’m just a little unsure, cause we’re here within a static function in a static class.

public static class LabelExtensions
{
    public static Label BlinkText(this Label label, int duration)
    {
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        timer.Interval = duration;
        timer.Tick += (sender, e) =>
            {
                timer.Stop();
                label.Font = new Font(label.Font, label.Font.Style ^ FontStyle.Bold);
            };

        label.Font = new Font(label.Font, label.Font.Style | FontStyle.Bold);
        timer.Start();

        return label;
    }
}

Update

Just to clarify i used System.Windows.Forms.Timer. So from your answers it seems, that especially using this timer class just was the right choice cause it does anything the way as i would expect it in this case. If you try another timer class in this case you can run into trouble as Matthew found out.
Also i found a way by using WeakReferences if my objects are staying alive or not.

Update 2

After a little sleep and a little more thinking, i also made another change to my tester (answer below) i just added a GC.Collect() after the last line and set the duration to 10000. After starting the BlinkText() several times i hitted all the time my button2 to get the current state and to force a garbage collection. And as it seems all the timers will be destroyed after calling the Stop() method. So also a garbage collection while my BlinkText is already left and the timer is running doesn’t lead to any problems.

So after all your good responses and a little more testing i can happily say that it just does what it should without leaving timers in the memory nor throwing away the timers before they done their work.

  • 1 1 Answer
  • 1 View
  • 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-14T05:02:25+00:00Added an answer on May 14, 2026 at 5:02 am

    After reading all your answers i just found a way to better test it.

    I updated my Extension class the following way:

    public static class LabelExtensions
    {
        public static List<WeakReference> _References = new List<WeakReference>();
    
        public static Label BlinkText(this Label label, int duration)
        {
            Timer timer = new Timer();
    
            _References.Add(new WeakReference(timer));
    
            timer.Interval = duration;
            timer.Tick += (sender, e) =>
            {
                timer.Stop();
                label.Font = new Font(label.Font, label.Font.Style ^ FontStyle.Bold);
            };
    
            label.Font = new Font(label.Font, label.Font.Style | FontStyle.Bold);
            timer.Start();
    
            return label;
        }
    }
    

    Also i created a second button, a second label and added the following code to the click event:

        private void button2_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
    
            for (int i = 0; i < LabelExtensions._References.Count; i++)
            {
                var wr = LabelExtensions._References[i];
                sb.AppendLine(i + " alive: " + wr.IsAlive);
            }
    
            label2.Text = sb.ToString();
        }
    

    Now i just pushed the first button several times to let the first label blink. Then i pressed my second button and got a list where i can see if my timers are still alive. At a first click they all got a true. But then i hit my first button several times again and when i updated my status message i saw that the first items are already got a false in IsAlive. So i can safely say that this function doesn’t lead to any memory problems.

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

Sidebar

Related Questions

I have touched on this question here , where Christopher gave an answer to
This IS NOT homework, but it is a practice that the teacher gave us
I recently asked this question: Compiler error referencing custom C# extension method Marc Gravell
I'm just starting with The Well-Grounded Rubyist, and they gave the following example: print
Just checking my JS and I have an error, but I cannot see where.
Just a knowledge question which I would like to ask: For example, I have
I've noticed that in trying to get seemingly simple node packages to install with
As background, I gave an answer to this post a little while ago: Return
I tried to find the answer to my problem in the questions history but
I'm trying to compile some software I've been writing in Linux that uses some

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.