473,378 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Why's this timer never stop?

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() );
}
}
}
Aug 8 '06 #1
6 8337
Hello, Michael!

When you create and start timer, Timer object is "pinned" via GCHandle.Alloc in
the internals of Timer.Enabled property.
That is why Timer object is not destroyed.

And now, about question why it is generating ticks. Timer internally uses Win32 API
function SetTimer
( http://msdn.microsoft.com/library/de...s/settimer.asp )

After calling SetTimer - window that creaeted timer will receive WM_TIMER windows message.
These messages will be sent until you call Timer.Stop, which will call KillTimer internally.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Aug 8 '06 #2
"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:ea**************@TK2MSFTNGP02.phx.gbl...
Hello, Michael!

When you create and start timer, Timer object is "pinned" via
GCHandle.Alloc in
the internals of Timer.Enabled property.
That is why Timer object is not destroyed.
Thanks for the reply, that makes sense but is there any reason they do it
that way? Are there any other classes in the framework that use a similar
technique?
>
And now, about question why it is generating ticks. Timer internally uses
Win32 API
function SetTimer
(
http://msdn.microsoft.com/library/de...s/settimer.asp )

After calling SetTimer - window that creaeted timer will receive WM_TIMER
windows message.
These messages will be sent until you call Timer.Stop, which will call
KillTimer internally.
I know the SetTimer api reasonably well and can't think of any reason they'd
need to pin the managed class when creating a wrapper.

Michael
Aug 9 '06 #3
Hello, Michael!

MCThanks for the reply, that makes sense but is there any reason they do
MCit that way? Are there any other classes in the framework that use a
MCsimilar technique?

Yes, there are. Using Reflector tool you can observer how many classes use GCHandle.Alloc(...)

MCI know the SetTimer api reasonably well and can't think of any reason
MCthey'd need to pin the managed class when creating a wrapper.

IMO if they wouldn't pin that class - there would be no guarantee that WM_TIMER
message handler will not cause exception.

If timer object will be "collected" by GC and KillTimer will not be called, who then will handle WM_TIMER?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Aug 9 '06 #4
"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
Yes, there are. Using Reflector tool you can observer how many classes use
GCHandle.Alloc(...)
Cool, I never realised you could use the "Used By" feature in reflector on
function calls. I've only ever used it on objects.
IMO if they wouldn't pin that class - there would be no guarantee that
WM_TIMER
message handler will not cause exception.

If timer object will be "collected" by GC and KillTimer will not be
called, who then will handle WM_TIMER?
Wouldn't the GC cause the finalizer to be called which could call KillTimer?

Michael
Aug 9 '06 #5
Hello, Michael!

MCWouldn't the GC cause the finalizer to be called which could call
MCKillTimer?

Timer class has only Dispose method that can cause KillTimer call.
Its base class ( Component ) has finalizer, but it calls Dispose with false parameter.
That result in timer.Enabled=false not beeing called,
that's why when Timer object is collected KillTimer will not be called.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Aug 9 '06 #6
"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:OM**************@TK2MSFTNGP03.phx.gbl...
Hello, Michael!

MCWouldn't the GC cause the finalizer to be called which could call
MCKillTimer?

Timer class has only Dispose method that can cause KillTimer call.
Its base class ( Component ) has finalizer, but it calls Dispose with
false parameter.
That result in timer.Enabled=false not beeing called,
that's why when Timer object is collected KillTimer will not be called.
Couldn't they have just called KillTimer still?
>
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Aug 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Phelan Cummings | last post by:
It use to work. I created numerous coppies of my MS Access2003 developed application for distribution using both the new Startup and Package and Deployment wizzards (that also incudes autorun...
1
by: Mike Hanson | last post by:
Are you sure the event is not firing, I have re-created your Web Form as best I can with the information provided and the event fires ok, but it still does not write anything to the page. Not...
8
by: TheMadHatter | last post by:
Yello, Quick Q: I created a windows service that changes some data every (approx) 1sec, but currently it is using a loop on a separate thread that uses Thread.Sleep(1000). Is that bad...
2
by: cntams | last post by:
All, I have a Windows Service and it has one System.Timers.Timer that fires every 500 milliseconds. Now I have noticed that there's a bug in System.Timers.Timer when it's being used combined with...
3
by: cj | last post by:
This program is used to send files to an ftp server at 2:00am each day. At the very top of my program I dim a new instance of a com FTP control. I have a processing sub which calls a login function...
5
by: John A. Bailo | last post by:
From a Windows service (NET 2.0) I want to launch serveral threads in a for loop that invokes a method using: new Thread(delegate() { myMethod(248);}).Start(); Will those threads stay...
7
by: Daniele Piccinini | last post by:
Hallo, I've writed a C# windows service in VS 2003. This service use a System.Timers.Timer to periodically call some functions of a web service. ( Some of this functions required minutes to be...
1
by: pantagruel | last post by:
Hi, I have an array like the following: for(x=0;x<results.length;x++){ alert(results.length); extracted=results.shift(); alert(results.length); if(results.indexOf(extracted)== -1){
10
by: Zytan | last post by:
I made a program using the Timer class, and I start the timer with Timer.Start(), but don't stop it with Timer.Stop(), and I assumed this was ok, but, Process Explorer informs me, after running it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.