user602996
user602996

Reputation:

C# Executing a method with intervals using system.threading.timer

Hello Lets say I have a console application, that looks like this

class Program
{
    static void Main(string[] args)
    {

    }

    public void DoSomething()
    {
        Console.WriteLine("Hello World");
    }
}

}

I wan't to execute my DoSomething method every 10'th second by using System.Threading.timer. Can anyone give an example of how that is done?

Thanks in advance :)

Upvotes: 0

Views: 2775

Answers (2)

Uwe Keim
Uwe Keim

Reputation: 40756

The documentation page for the System.Threading.Timer class has a lengthy, good example.

Upvotes: 1

Darren Young
Darren Young

Reputation: 11100

Timer timer1 = new Timer(10000);
        timer1.Enabled = true;
        timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
        timer1.Start();



    static void timer1_Elapsed(object sender, ElapsedEventArgs e)
    {
       //Do Something

    }

Upvotes: 1

Related Questions