【发布时间】:2021-07-12 08:06:24
【问题描述】:
protected override void OnStart(string[] args)
{
aTimer = new Timer();
var timer = System.Configuration.ConfigurationManager.AppSettings["Timer"].ToString(); //Appconfig "03:00"
var date = DateTime.Now;
DateTime scheduled = DateTime.ParseExact(timer, "HH:mm",
CultureInfo.InvariantCulture);
if (date > scheduled) scheduled = scheduled.AddDays(1);
var test = scheduled.Subtract(date).TotalMinutes;
aTimer.Interval = test;
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
aTimer.AutoReset = false;
}
上面的代码在启动时第一次运行,然后即使间隔设置为每天凌晨 3:00 运行,也不再运行。
更新: 效果很好。我想知道如果我将计时器设置为每 1 秒运行一次,它是否有可能与每个事件重叠。是否等待事件结束后再开始新的事件?
【问题讨论】:
-
如果我有管理员权限,我会在 1 分钟内完成。看来我只能做 Windows 服务了。
-
我能够将其安装为本地服务
标签: c# windows service background scheduling