I just answered a fairly simple question using the extension method. But after writing it, I remembered that you cannot unsubscribe from a lambda from an event handler.
There is still no big problem. But how does all this behave in the extension method?
Below my code is disabled again. So can someone enlighten me if this leads to a lot of timers hanging in memory if you call this extension method several times?
I would say no, because the scope of the timer is limited by this function. Therefore, leaving it, no one has a link to this object. I am just a little sure, because we are here in 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
, System.Windows.Forms.Timer. , , , . , , .
, WeakReference, .
2
() GC.Collect() 10000. BlinkText() 2, . , , , Stop(). , BlinkText , , .
, , , , , .