System.Threading.Timer and Dotnet 2.0

S

Stig Hausberg

Hi folks!
I'm currently working in a prototype project as we are moving into dotnet
and I've have stumbled upon an issue someone hopefully can help me with.

The system we are building needs to run a specific part of code at a set of
intervals, lets say once an hour. As the system will be installed on
multiple servers we were hoping that we could avoid to use any sort of
applications/windows services and build this into the asp.net solution we
are creating.
I've been looking into using System.Threading.Timer but it appears as the
garbage collector collects the thread when the refernece dies if I use
subsequent executions (It usually happens after 15-30 sec.). But if I only
invoke the thread once in the future it appears to have a longer timespan
(I've tried up to 6 minutes).

As I'm relative fresh to the more advanced features of dotnet I would
appriciate it if someone enlightened me about this subject or/and could
point me in a direction(best practice) which could help me solve this
problem.

Regards
Stig Hausberg
 
P

Patrice

I'm not sure I really understand your description....

"the garbage collector collects the thread when the refernece dies" : do you
mean that the timer will not fire because the thread that create the timer
is no more here te be warned...
"But if I only invoke the thread once in the future" : or you already create
another thread ????

What does this code ? If this is some database handling it could be a a job
scheduled on the DBMS side.
If a service annoys you, it could be also just a regular EXE file that would
be launched with the scheduling service.

IMO stuffing this kind of things in an ASP.NET application is quite a hack
(for example what if the ASP.NET application is recycled, you'll likely have
to maintain the ASP.NET application always alive etc...)

Patrice
 
K

Kevin Spencer

The lifetime of a timer is limited to the lifetime of the class which
instantiates it. If you're talking about a Page class, you're talking about
a lifetime measured in milliseconds. A Page class exists for the length of
time it takes to Respond to a single Request for that Page.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
Joined
Aug 3, 2007
Messages
1
Reaction score
0
Threading.Timer problem

I also face the same problem in windows service my threading.Timer fires once then then never fires for different intervals. Here is the code

for (int iIndex = 0; iIndex < ds.Tables["Table1"].Rows.Count; iIndex++)
try
{
lintInterval = int.Parse(ds.Tables["Table1"].Rows[iIndex]["SearchInterval"].ToString()) ;
TimeSpan oSpan = TimeSpan.FromMinutes(lintInterval);
System.Threading.Timer oTimer = new System.Threading.Timer(new System.Threading.TimerCallback(startCompare1),
(object) iIndex, oSpan, TimeSpan.FromMilliseconds(-1));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top