Is there a timer class in C# that isn't in the Windows.Forms namespace?

后端 未结 6 1267

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 f

相关标签:
6条回答
  • 2020-12-30 03:48

    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.

    0 讨论(0)
  • 2020-12-30 03:53

    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.

    0 讨论(0)
  • 2020-12-30 03:53

    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

    0 讨论(0)
  • 2020-12-30 03:53

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

    0 讨论(0)
  • 2020-12-30 03:57

    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

    0 讨论(0)
  • 2020-12-30 03:59

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

    0 讨论(0)
提交回复
热议问题