What's the best Timer to use

D

DaTurk

Hi,

I'm creating an application that will need to use a timer to do a
static method call every second. My question, is what would be the
best timer to use? I know there are several, but I'd like to use a
timer that is probably the most reliable, and hopefully designed with
high performance in mind. Thank you in advance.
 
N

Nicholas Paldino [.NET/C# MVP]

DaTurk,

What you want is the Timer class in the System.Timers class.

Hope this helps.
 
D

DaTurk

DaTurk,

What you want is the Timer class in the System.Timers class.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I'm creating an application that will need to use a timer to do a
static method call every second. My question, is what would be the
best timer to use? I know there are several, but I'd like to use a
timer that is probably the most reliable, and hopefully designed with
high performance in mind. Thank you in advance.- Hide quoted text -

- Show quoted text -

Would System.Timer be the best choice? I need the most reliable, and
high performance.
 
W

William Stacey [C# MVP]

I always use system.threading.timer with good luck. I create a new timer
each time to head off a few issues (i.e. 49 day issue, and overlapped jobs).

public void DoIt()
{
Timer t = null;
t = new Timer(
delegate(object state)
{
t.Dispose(); // t is "captured", so we don't need to hold it
elsewhere.

// Run your stuff.

DoIt();
}, null, 1000, -1));
}

Looks like stack recursion, but is not as we start async timer and return.
We schedule a new timer each time and only after we run the work so we don't
have danger of overlapped jobs. However, the next job only starts after the
previous one is finished, so that could be more then 1 second. You can
wiggle the ms's as needed.

--
William Stacey [C# MVP]
PCR concurrency library: www.codeplex.com/pcr
PSH Scripts Project www.codeplex.com/psobject


| On Mar 30, 3:43 pm, "Nicholas Paldino [.NET/C# MVP]"
| > DaTurk,
| >
| > What you want is the Timer class in the System.Timers class.
| >
| > Hope this helps.
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - (e-mail address removed)
| >
| >
| > | >
| >
| >
| > > Hi,
| >
| > > I'm creating an application that will need to use a timer to do a
| > > static method call every second. My question, is what would be the
| > > best timer to use? I know there are several, but I'd like to use a
| > > timer that is probably the most reliable, and hopefully designed with
| > > high performance in mind. Thank you in advance.- Hide quoted text -
| >
| > - Show quoted text -
|
| Would System.Timer be the best choice? I need the most reliable, and
| high performance.
|
 
G

gshzheng

In the book CLR via C# ,Jeffery Richter,

3 Timers are described in detail.

1.System.Threading.Timer
2.System.Windows.Forms.Timer
3.System.Timers.Timer

System.Threading.Timer uses a thread getting form ThreadPool.
System.Windows.Forms.Timer works with WM_TIMER message.
System.Timers.Timer may be seen as a encapsulation of System.Threading.Timer
..
And Jeffery Richter suggested us NOT to use this cause it possiblly be
remove form FCL.

System.Threading.Timer is choosed firstly.

gshzheng
20070331



DaTurk said:
DaTurk,

What you want is the Timer class in the System.Timers class.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I'm creating an application that will need to use a timer to do a
static method call every second. My question, is what would be the
best timer to use? I know there are several, but I'd like to use a
timer that is probably the most reliable, and hopefully designed with
high performance in mind. Thank you in advance.- Hide quoted text -

- Show quoted text -

Would System.Timer be the best choice? I need the most reliable, and
high performance.
 
W

Willy Denoyette [MVP]

gshzheng said:
In the book CLR via C# ,Jeffery Richter,

3 Timers are described in detail.

1.System.Threading.Timer
2.System.Windows.Forms.Timer
3.System.Timers.Timer

System.Threading.Timer uses a thread getting form ThreadPool.
System.Windows.Forms.Timer works with WM_TIMER message.
System.Timers.Timer may be seen as a encapsulation of System.Threading.Timer .
And Jeffery Richter suggested us NOT to use this cause it possiblly be remove form FCL.

While it's true that you better ignore System.Timers.Timer and use System.Threading.Timer
instead, no-one ever said that the former would go away any time soon.

Willy.
 
J

John Vottero

William Stacey said:
I always use system.threading.timer with good luck. I create a new timer
each time to head off a few issues (i.e. 49 day issue, and overlapped
jobs).

What is the 49 day issue?
 
R

Rad [Visual C# MVP]

Hi,

I'm creating an application that will need to use a timer to do a
static method call every second. My question, is what would be the
best timer to use? I know there are several, but I'd like to use a
timer that is probably the most reliable, and hopefully designed with
high performance in mind. Thank you in advance.

I've had very good reliability with system.threading.timer. I've used it in
a couple of windows services and it has been very reliable
 
W

William Stacey [C# MVP]

Reaching from memory, but I have read issues people had where the timer
overflows and stops working after 49 days if recurring timer and 1 sec
interval.

--
William Stacey [C# MVP]


| | >I always use system.threading.timer with good luck. I create a new timer
| > each time to head off a few issues (i.e. 49 day issue, and overlapped
| > jobs).
|
| What is the 49 day issue?
|
|
 
M

Michael D. Ober

The 49 day issue is that a 32 bit millisecond counter overflows and wraps to
zero every 49.7 days. Apparently the .Net 1.x is broken in this regards,
despite the fact the MS fixed it in the underlying OS years earlier. I
haven't heard of any problems in .Net 2.0.

Mike Ober.
 

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

Top