473,382 Members | 1,204 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,382 software developers and data experts.

Systereading.Timer class question

The follwowing remarks taken from the MSDN:

As long as you are using a Timer, you must keep a reference to it. As with
any managed object, a Timer is subject to garbage collection when there are
no references to it. The fact that a Timer is still active does not prevent
it from being collected.

What does this mean?
If I have the following:

class Sample1 : System
{
DataTable dt = new DataTable ();

public process ()
{
while (true)
{

do something
}
}

}

can the dt be delete by the garbage collection?


Nov 16 '05 #1
3 1111
Any managed object is subject to data collection when there are no more
references to it. In your example, the dt member will be garbage collected
at some point after there are no more references to the runtime instance of
Sample1 (unless you have a method of Sample1 that returns a copy of the dt
reference to something outside the class, in which case there might be other
references that would keep it from being garbage-collected even if the
Sample1 instance is destroyed).

--Bob

"R.A." <te**@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
The follwowing remarks taken from the MSDN:

As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.

What does this mean?
If I have the following:

class Sample1 : System
{
DataTable dt = new DataTable ();

public process ()
{
while (true)
{

do something
}
}

}

can the dt be delete by the garbage collection?

Nov 16 '05 #2
The follwowing remarks taken from the MSDN:

As long as you are using a Timer, you must keep a reference to it. As with
any managed object, a Timer is subject to garbage collection when there are
no references to it. The fact that a Timer is still active does not prevent
it from being collected.

What does this mean?
It means exactly what it says. If you for example do

void StartATimer()
{
Timer t = new Timer(new TimerCallback(Handler), null, 0, 1000);
}

the timer will eventually be garbage collected and stop calling
Handler, since you don't hold a reference to the object after the
StartATimer method returns.
If I have the following:

class Sample1 : System
{
DataTable dt = new DataTable ();

public process ()
{
while (true)
{

do something
}
}

}

can the dt be delete by the garbage collection?


Not before the Sample1 object itself is eligible for garbage
collection.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
Hi,
what does that mean is:

public void MyMethod()
{
if (somecond)
{
Timer t = new Timer(new TimerCallback(Handler), null, 0, 100);
}

while (something)
{ do something}
}

The reference to the timer object t goes out of scope when you exit the
"if" block. So there is no more references to that timer, and it willl
be garbage colected at some point of time, even if it is still running.

Sunny
In article <#g**************@TK2MSFTNGP11.phx.gbl>, te**@hotmail.com
says...
The follwowing remarks taken from the MSDN:

As long as you are using a Timer, you must keep a reference to it. As with
any managed object, a Timer is subject to garbage collection when there are
no references to it. The fact that a Timer is still active does not prevent
it from being collected.

What does this mean?
If I have the following:

class Sample1 : System
{
DataTable dt = new DataTable ();

public process ()
{
while (true)
{

do something
}
}

}

can the dt be delete by the garbage collection?


Nov 16 '05 #4

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

Similar topics

4
by: Hagay Lupesko | last post by:
Hi, I've encountered a strange phenomena which appears to me as a bug: I have an engine that uses a System.Threading.Timer to invoke a delegate every X minutes. The code looks something...
2
by: Benjamin | last post by:
Hi, I am having a problem enabling a timer in my class. I have attached some sample pseudo code (see "Sample code illustrating issue") so you can see what I am talking about. Whats the issue?...
1
by: Chuck | last post by:
I am creating a pub/sub broker implementation in C# and I am having some trouble understanding how I should best implement the invoke portion in order to make sure it is as thread-safe as it can...
3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
4
by: VB Programmer | last post by:
I'm creating a console application. Can I use a timer control to do something every 5 seconds?
3
by: rodchar | last post by:
hey all, if i have a timer program that ticks every 1/100th of a second and it just checks a variable status would that be a huge hit on the performance of the system or whatever indicator the...
5
by: Tom | last post by:
Using multiple System.Timers.Timer objects in a Windows Service for performing multi-thread activities in a periodic fashion. Timers are AutoReset=false, to only have a single timer execution...
5
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to...
13
by: Jordan | last post by:
All, I have a UI form calling a class object that contains a timer that routinely draws intensive information to the screen (~30 fps). The drawing is invoked on the main UI thread. I need the...
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.