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

System.Threading.Timers question

I'm running into an issue where I have a timer that appears to be timing
out immediately, when it shouldn't be timing out for about int.MaxValue
milliseconds. I have written a small app that isolates the problem and
was wondering if anyone had any ideas on what I am doing wrong, or if
there is some known bug, or I am exceeding some known maximum (which I
can't seen to find in documentation anywhere...)

Anyway, the following code used to appear to work fine. I didn't let it
run for the full 24 days, but the timer function didn't execute after
letting it run for a few hours. Now, the timer function gets executed
immediately. Here's the code:

using System;
class Test
{
static void Main(string[] args)
{
try
{
TimeSpan oSpan = TimeSpan.FromMilliseconds(int.MaxValue);

Console.WriteLine("Waiting for {0}", oSpan);

System.Threading.Timer oTimer = new System.Threading.Timer(
new System.Threading.TimerCallback(Thread),
null, oSpan, TimeSpan.FromMilliseconds(-1));

while(true);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}
}

static void Thread(object o)
{
Console.WriteLine("Thread...");
}
}
Nov 17 '05 #1
5 6562
Deiussum,

I've run this on .NET 2.0 (beta 1), and it doesn't give me any problems.
It basically starts waiting. I'm assuming after a day it will fire.

What version of .NET are you running? From what I understand of the
documentation, the timer should NOT fire immediately, and fire every day or
so.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Deiussum" <de******@lycanth.com> wrote in message
news:ux*************@TK2MSFTNGP12.phx.gbl...
I'm running into an issue where I have a timer that appears to be timing
out immediately, when it shouldn't be timing out for about int.MaxValue
milliseconds. I have written a small app that isolates the problem and
was wondering if anyone had any ideas on what I am doing wrong, or if
there is some known bug, or I am exceeding some known maximum (which I
can't seen to find in documentation anywhere...)

Anyway, the following code used to appear to work fine. I didn't let it
run for the full 24 days, but the timer function didn't execute after
letting it run for a few hours. Now, the timer function gets executed
immediately. Here's the code:

using System;
class Test
{
static void Main(string[] args)
{
try
{
TimeSpan oSpan = TimeSpan.FromMilliseconds(int.MaxValue);

Console.WriteLine("Waiting for {0}", oSpan);

System.Threading.Timer oTimer = new System.Threading.Timer(
new System.Threading.TimerCallback(Thread),
null, oSpan, TimeSpan.FromMilliseconds(-1));

while(true);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}
}

static void Thread(object o)
{
Console.WriteLine("Thread...");
}
}

Nov 17 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Deiussum,

I've run this on .NET 2.0 (beta 1), and it doesn't give me any problems.
It basically starts waiting. I'm assuming after a day it will fire.

What version of .NET are you running? From what I understand of the
documentation, the timer should NOT fire immediately, and fire every day or
so.


I'm running .Net 1.1. (The specific version reported in the control
panel 1.1.4322.573)

This is for enterprise software that we are developing, and won't likely
be updating to .Net 2.0 until it is well out of beta. Our QA team had
noted this problem awhile ago, but I was never able to reproduce it on
my own machine until recently. I'm guessing that possibly some of the
recent .Net fixes changed something?

The timespan in question is actually a bit over 24 days (24.20:31:23.647
to be exact.) I used -1 milliseconds for the repeat time, which MSDN
states to use if you do not want the periodic firing of the timer. I
just want it to fire once, but not until after the timeout time. :)

I also had tried using the overload that takes a uint representing the
number of milliseconds and got the same results.
Nov 17 '05 #3
It's gotten more interesting... In an attempt to find a lower maximum
to limit the timer to, I was trying to find out at what exact point the
problem occurs. The results are interesting...

I started to narrow it down to a timespan of:

TimeSpan oSpan = new TimeSpan(9, 17, 45, 0, 0);

I started trying to narrow down the seconds next, and found that after a
bit, the above timespan also showed the same problem, and I had to
change the timespan to:

TimeSpan oSpan = new TimeSpan(9, 17, 43, 0, 0);

Then a few minutes later, it had to be dropped down to:

TimeSpan oSpan = new TimeSpan(9, 17, 37, 0, 0);

Something definitely seems fishy about this...
Nov 17 '05 #4
I now have a theory on why this problem happens... I tried my test app
again this morning and the timer was expiring immediately at about
2005-04-07 04:21:0.000, but not at 04:20:0.000. (A timespan of rougly
7.20:04:00)

I then rebooted, tried it again, and now it all works.

My theory on what the bug is is this: Internally, the
System.Threading.Timers uses the number of milliseconds since the system
was last rebooted, and calculates what that value will be by the time it
should fire, using something like timeGetTime. According to MSDN, this
value wraps every 2^32 milliseconds, about 47.71 days. Since I seldom
reboot my machine, it has probably been running roughly 40 days, thus
when it tries to calculate the millisecond value since restart, it will
wrap, causing it to be LESS than the current millisecond value, and
therefore the timer will fire immediately.

--
Dan Jenkins <de******@lycanth.com>
Senior Software Engineer
Echelon Fargo
Nov 17 '05 #5
I've run into exactly this problem and am hoping to hear from Microsoft
whether it's a confirmed problem in .NET, perhaps with a fix in 2.0.

Basically, the two background System.Threading.Timers in our ASP.NET
application start freaking out when OS uptime reaches exactly 2^32
milliseconds (49.7 days). At that moment, timers that were set to fire
every 15 minutes start firing every 1 second.

This is on Windows Server 2003, .NET 1.1 SP1, IIS 6.0.

Thanks.
"Deiussum" wrote:
I now have a theory on why this problem happens... I tried my test app
again this morning and the timer was expiring immediately at about
2005-04-07 04:21:0.000, but not at 04:20:0.000. (A timespan of rougly
7.20:04:00)

I then rebooted, tried it again, and now it all works.

My theory on what the bug is is this: Internally, the
System.Threading.Timers uses the number of milliseconds since the system
was last rebooted, and calculates what that value will be by the time it
should fire, using something like timeGetTime. According to MSDN, this
value wraps every 2^32 milliseconds, about 47.71 days. Since I seldom
reboot my machine, it has probably been running roughly 40 days, thus
when it tries to calculate the millisecond value since restart, it will
wrap, causing it to be LESS than the current millisecond value, and
therefore the timer will fire immediately.

--
Dan Jenkins <de******@lycanth.com>
Senior Software Engineer
Echelon Fargo

Nov 17 '05 #6

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

Similar topics

0
by: Thana N. | last post by:
Hi All, I've found that kbAlert notify that "System.Threading.Timers Class Library may unexpectedly run in the .NET Framework 1.1" (http://www.kbalertz.com/Feedback_843561.aspx). I would like...
3
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan...
1
by: Tom | last post by:
I've googled, and read, and stripped out code, and rewritten code, and still can't get a System.Threading.Timer to work. (I hereby publicly admit that I'm a failure here...) Could someone please...
3
by: Kenny | last post by:
I am running a windows service that takes actions based on a couple System.Threading.Timers. The intervals are usually short... based on the time of day, anywhere between 1 and 5 minutes. ...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
0
by: Kelsang Wangchuk | last post by:
Hi Just a quick question... When would you use System.Timers.Timer, and when System.Threading.Timer? What are the principal differences between them? There is a lot of discussion about...
1
by: james.e.coleman | last post by:
Hello, System.Timers.Timer/System.Threading.Timer do not fire on my development server, yet they work perfectly on the production server. I would appreciate any help on what the cause of this...
0
by: Bruce | last post by:
Hi I have a question on using System.Threading.Timer. I understand that in order for the timer to work, I need to store a reference to the timer so that it does not get garbage collected. In my...
2
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've two questions regarding the System.Threading.Thread namespace. First, what is the point of having a ThreadStart parameter? For example... using System.Threading;...
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
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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.