在特定日期和时间使用计时器触发事件

fir*_*and 2 c# timer scheduled-tasks

我正在使用System.Timer来触发事件.目前我每隔1小时触发一次,检查它是否与配置的值(日,时间)匹配.

但是有可能在特定时间触发它吗?比如假设在周日的12Am.

Windows任务计划程序更合适,但它不是一个选项.

提前致谢

Han*_*ant 9

目前尚不清楚为什么你不将计时器的间隔设置为目标日期/时间.毫秒数有限制,最长可达2 ^ 31毫秒,27天.只要你能留在那个范围内,你就会很好.

    private static void SetTimer(Timer timer, DateTime due) {
        var ts = due - DateTime.Now;
        timer.Interval = ts.TotalMilliseconds;
        timer.AutoReset = false;
        timer.Start();
    }
Run Code Online (Sandbox Code Playgroud)