M
Michael C
I've created a timer object and set it running without keeping a reference
to it and not stopping it. Shouldn't the garbage collector pick up that
there's no reference to the timer, call finalize and hence stop the timer?
Before anyone says 'just call dispose' I'm just testing to try to work out
what's going on with creating/destroying forms and controls etc.
Code sample is below. Thanks for any replies
Michael
using System;
using System.Windows.Forms;
namespace TestStuff
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnCollect;
public Form1()
{
this.btnStart = new System.Windows.Forms.Button();
this.btnCollect = new System.Windows.Forms.Button();
this.SuspendLayout();
this.btnStart.Location = new System.Drawing.Point(176, 56);
this.btnStart.Size = new System.Drawing.Size(80, 24);
this.btnStart.Text = "Start";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
this.btnCollect.Location = new System.Drawing.Point(176, 88);
this.btnCollect.Size = new System.Drawing.Size(80, 24);
this.btnCollect.Text = "GC.Collect";
this.btnCollect.Click += new System.EventHandler(this.btnCollect_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.btnCollect);
this.Controls.Add(this.btnStart);
this.ResumeLayout(false);
}
private void btnCollect_Click(object sender, System.EventArgs e)
{
GC.Collect();
}
private void btnStart_Click(object sender, System.EventArgs e)
{
Timer t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 500;
t.Start();
t = null;
}
private void t_Tick(object sender, EventArgs e)
{
Console.WriteLine(Environment.TickCount.ToString());
}
}
}
to it and not stopping it. Shouldn't the garbage collector pick up that
there's no reference to the timer, call finalize and hence stop the timer?
Before anyone says 'just call dispose' I'm just testing to try to work out
what's going on with creating/destroying forms and controls etc.
Code sample is below. Thanks for any replies
Michael
using System;
using System.Windows.Forms;
namespace TestStuff
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnCollect;
public Form1()
{
this.btnStart = new System.Windows.Forms.Button();
this.btnCollect = new System.Windows.Forms.Button();
this.SuspendLayout();
this.btnStart.Location = new System.Drawing.Point(176, 56);
this.btnStart.Size = new System.Drawing.Size(80, 24);
this.btnStart.Text = "Start";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
this.btnCollect.Location = new System.Drawing.Point(176, 88);
this.btnCollect.Size = new System.Drawing.Size(80, 24);
this.btnCollect.Text = "GC.Collect";
this.btnCollect.Click += new System.EventHandler(this.btnCollect_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.btnCollect);
this.Controls.Add(this.btnStart);
this.ResumeLayout(false);
}
private void btnCollect_Click(object sender, System.EventArgs e)
{
GC.Collect();
}
private void btnStart_Click(object sender, System.EventArgs e)
{
Timer t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 500;
t.Start();
t = null;
}
private void t_Tick(object sender, EventArgs e)
{
Console.WriteLine(Environment.TickCount.ToString());
}
}
}