简体   繁体   中英

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 :)

System.Threading.Timer文档页面有一个冗长的好例子。

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM