【问题标题】:How to tick in .NET Core? Timespan? Ticker?如何在 .NET Core 中打勾?时间跨度?代号?
【发布时间】:2017-04-12 08:50:04
【问题描述】:

我习惯了旧的 WCF 计时器/计时方式。例如,我希望每 25 秒调用一次某个方法。如何在 .NET Core 中执行此操作?

【问题讨论】:

  • tick 是什么意思?每隔x 秒做点什么?

标签: .net-core timer timespan


【解决方案1】:

您可以使用System.Threading.Timer。请注意,此 Timer 不是线程安全的 - 有关详细信息,请参阅此问题:Timer (System.Threading) thread safety

namespace TimerApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (new System.Threading.Timer(DoSomething, null , 0, 250))
            {
                // do something else
                Thread.Sleep(TimeSpan.FromDays(1));
            }
        }

        public static void DoSomething(object state)
        {
            // called every 250ms
            Console.WriteLine(DateTime.Now);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-11-13
    • 2012-04-12
    • 2020-09-12
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2012-06-18
    相关资源
    最近更新 更多