Question

I want to use a timer in my simple .NET application written in C#. The only one I can find is the Windows.Forms.Timer class. I don't want to reference this namespace just for my console application.

Is there a C# timer (or timer like) class for use in console applications?

Was it helpful?

Solution

System.Timers.Timer

And as MagicKat says:

System.Threading.Timer

You can see the differences here: http://intellitect.com/system-windows-forms-timer-vs-system-threading-timer-vs-system-timers-timer/

And you can see MSDN examples here:

http://msdn.microsoft.com/en-us/library/system.timers.timer(VS.80).aspx

And here:

http://msdn.microsoft.com/en-us/library/system.threading.timer(VS.80).aspx

OTHER TIPS

I would recommend the Timer class in the System.Timers namespace. Also of interest, the Timer class in the System.Threading namespace.

using System;
using System.Timers;

public class Timer1
{
    private static Timer aTimer = new System.Timers.Timer(10000);

    public static void Main()
    {
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();
    }

    // Specify what you want to happen when the Elapsed event is 
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}

Example from MSDN docs.

There are at least the System.Timers.Timer and System.Threading.Timer classes that I'm aware of.

One thing to watch out though (if you haven't done this before already), say if you already have the System.Threading namespace in your using clause already but you actually want to use the timer in System.Timers you need to do this:

using System.Threading;
using Timer = System.Timers.Timer;

Jon Skeet has an article just on Timers in his multithreading guide, it's well worth a read: http://www.yoda.arachsys.com/csharp/threads/timers.shtml

This question is old and existing answers cover well the issue as it was at the time. In the meantime, something new has happened.

Timer, sure, for what ?

Using a timer is a way to run some processing with some delay and/or at regular interval. There are two cases:

(1) it's just to run some short code periodically without any thread concern, no trouble, no mess. If the plain Windows Forms timer is not suitable, then System.Timers.Timer with its SynchronizingObject property makes it more straightforward than System.Threading.Timer .

(2) what you're coding is in the realm of asynchronous concurrent processing. That has traditionally been error-prone, difficult to debug, to get right, whatever the plain timer used.

In case 2 you might get away with traditional approach, but beware, complexity is lurking and ready to eat you not just once but any time you just don't make the best choice, with cumulative effects.

Now we've got something better

If your situation deals with some kind of "event" handling (whatever the way it's coded: keypresses, mouse buttons, bytes from a serial port, from a network connection, from measurements, etc), you should consider Reactive Programming.

Reactive Programming has in recent years somehow uncovered how to code for these situations, while not falling into complexity traps.

So, technically the following link is an answer to the question: it is a timer which is in the System.Reactive.Linq namespace: Observable.Timer Method (System.Reactive.Linq)

To be fair, it's a timer that comes and plays well with the Reactive Programming mindset and a lot of game-changing stuff. Yet it might or might not be the best tool, depending on the context.

Since this question is .NET-centric, you might be interested in Good introduction to the .NET Reactive Framework

Or for a clear, illustrated, more general (not Microsoft-centric) document, this seems good The introduction to Reactive Programming you've been missing.

System.Diagnostics.Stopwatch if your goal is to time how long something takes to run

It is recommended to not to use System.Timer's Timer class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top