我有应用程序每隔一分钟通过Timer
执行一些任务,我想添加选项,以便在应用程序运行过程中使用用户输入更改此任务。
例如,如果我的Timer.Tick
设置为1分钟并更改为1小时,Timer.Tick
属性将更新,我需要重新启动我的应用程序吗?
答案 0 :(得分:1)
我刚刚在LINQPad中运行了它,并且它已经打开了'我改变间隔后的每一秒,意味着它看起来像是立即尊重你对间隔属性的改变。
void Main()
{
var timer = new System.Windows.Forms.Timer();
timer.Tick += Timer_Tick;
timer.Interval = 50000;
timer.Start();
System.Threading.Thread.Sleep(1000);
timer.Interval = 1000;
}
private void Timer_Tick(object sender, EventArgs e)
{
Console.WriteLine("Tick");
}
我假设您使用System.Windows.Forms.Timer
基于您引用Tick
事件。我也使用System.Threading.Timer
运行相同的测试并看到相同的结果。