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

1,708

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
1,708
Saygın Karahan
Author by

Saygın Karahan

Updated on July 09, 2022

Comments

  • Saygın Karahan
    Saygın Karahan almost 2 years

    I'm using starling framework for my game project and it hasn't got any draw dashed line method. Because of this they suggest me to draw dashed lines with using small rectangles which is called quads.

    My math is not enough for it, could you give a sample method for rectangles with dashed lines occurring.

    Drawing dashed lines with using rectangles

    Thanks..

    • marbel82
      marbel82 almost 11 years
      Paint this horizontally in shape, then rotate shape.
    • Garry Wong
      Garry Wong almost 11 years
      What is preventing you from using Flash's own graphics class to draw the line? Take a look at help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/‌​… and the example shown for the moveTo() method , which shows how to do a dashed line.
  • Ren
    Ren almost 11 years
    Can you post the code? It may help others with similar issues in the future.