473,569 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Threadin g.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.FromMi lliseconds(int. MaxValue);

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

System.Threadin g.Timer oTimer = new System.Threadin g.Timer(
new System.Threadin g.TimerCallback (Thread),
null, oSpan, TimeSpan.FromMi lliseconds(-1));

while(true);
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}
finally
{
Console.WriteLi ne("Press enter to continue...");
Console.ReadLin e();
}
}

static void Thread(object o)
{
Console.WriteLi ne("Thread...") ;
}
}
Nov 17 '05 #1
5 6574
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.co m

"Deiussum" <de******@lycan th.com> wrote in message
news:ux******** *****@TK2MSFTNG P12.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.FromMi lliseconds(int. MaxValue);

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

System.Threadin g.Timer oTimer = new System.Threadin g.Timer(
new System.Threadin g.TimerCallback (Thread),
null, oSpan, TimeSpan.FromMi lliseconds(-1));

while(true);
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}
finally
{
Console.WriteLi ne("Press enter to continue...");
Console.ReadLin e();
}
}

static void Thread(object o)
{
Console.WriteLi ne("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.64 7
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.Threadin g.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******@lycan th.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.Threadin g.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.Threadin g.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******@lycan th.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
1132
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 to know how System.Threading.Timers relate to TickCount. And what are the unexpected errors? Thanks, Thana N.
3
5578
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 dueTime) is always set to 6 days, 23 hours, 59 minutes, .. Some weeks ?!?, the timer restarts immediately after its execution (and loop indefinitely)....
1
2272
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 take a quick look at this and tell me where I'm going wrong? My actual use is more complex, but when I couldn't get that to work I created a new...
3
2312
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. Recently however, the event that was firing based on the timer started firing rapidly; it fired about 9000 times in a minute and a half. After the minute...
2
13171
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 System.Timers.Timer but i had to remove it because of known bug(842739). Now i am using System.Threading.Timer. It executes routine fine but the...
0
5169
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 System.Timers.Timer and its use in running background server activities, but not much about System.Threading.Timer other than in the reference, which...
1
2384
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 could be. I am convinced it is not the code since the code works on another machine. Thanks,
0
1582
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 application, I create a lot of timers - so I have a StoreTimer class that has a method AddTimer that other classes can call. Inside AddTimer, I...
2
1683
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; Thread myThread; myThread = new Thread(new ThreadStart(myFunc)); // This works
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.