每隔x分钟调用一个方法

144

我想每5分钟调用一次某个方法。 我该怎么做?

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("*** calling MyMethod *** ");
        Console.ReadLine();
    }

    private MyMethod()
    {
        Console.WriteLine("*** Method is executed at {0} ***", DateTime.Now);
        Console.ReadLine();
    }
}

10
System.Threading.Timer:这是一个.NET Framework中的类,用于创建计时器对象,以在一段时间后执行指定的回调函数。 - user703016
1
或者 System.Timers.Timer - http://www.dotnetperls.com/timer - Sinan AKYAZICI
11个回答

-2
while (true)
{
    Thread.Sleep(60 * 5 * 1000);
    Console.WriteLine("*** calling MyMethod *** ");
    MyMethod();
}

1
如果需要的话,使用 await Task.Delay(60 * 5 * 1000); - CMS
我喜欢这个答案,比任何一个计时器都简单。 - Denny
我认为 sleep 会导致整个应用程序冻结! - Moslem Hadi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接