Public Group

# Timers in C#!

This topic is 5062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I am not exactly sure if this qualifies as a beginner's question but here goes it anyway... How can I get a hold of the Timer.Elapsed method to make my own. I am using System.Timers.Timer...am I supposed to use System.Windows.Forms.Timer? Any help is much appreciated. :)

##### Share on other sites
The System.Windows.Forms.Timer is preferred for windows GUI applications. The System.Timers.Timer is preferred for server applications or any application that isn't dependent on user interaction. They don't really say what the differences are in much detail. But, most likely the server version is a little more accurate. But, the forms version runs on the same thread as the windows event handler, which is needed if you are updating any windows or controls.

You write your own method to execute when the timer goes off by setting an event handler. The System.Forms.Timer has a Tick event. So you write a method

public void OnTimerTick(object sender, EventArgs event) {...}

Then, in your initialization, you attach the method to the event.

...timer.Tick += OnTimerTick;...

EDIT: Also, it works very similarly for the System.Timers.Timer. Only, System.Timers.Timer's event is called Elapsed instead of Tick.

##### Share on other sites
Yeah thanks. I figured it out... hehe It took me awhile to realize that the error was saying only works on the LEFT hand side of +=. Thought it was saying right hand side...heh and I had to pass this in: new System.Timers.ElapsedEventHandler(this.iTimer_Elapsed)

Thanks though!

##### Share on other sites
in case its any help, Windows.Timers are not thread safe whereas Timers.Timer is. took me many late nights of debugging to figure that one

##### Share on other sites
Also note that the windows form timer can be stopped or paused when a dialog box is displayed, This is done without programming the stop. If you need a timmer to run for the complete running time of your application I would use the System.Timers.Timer.

Check out the Quickstarts on www.gotdotnet.com for an example of using the system timer.

##### Share on other sites
Quote:
 Original post by Krischeh and I had to pass this in: new System.Timers.ElapsedEventHandler(this.iTimer_Elapsed)

nimrand uses what is called an inferred delegate, i.e. simply using the method name in places where a delegate is needed. The compiler will implicitly create the corresponding delegate. This is a new feature in C# 2

Regards,
Andre

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 10
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011395
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!