是否可以在运行过程中更改Timer Tick

时间:2014-04-09 14:34:53

标签: c# .net timer

我有应用程序每隔一分钟通过Timer执行一些任务,我想添加选项,以便在应用程序运行过程中使用用户输入更改此任务。 例如,如果我的Timer.Tick设置为1分钟并更改为1小时,Timer.Tick属性将更新,我需要重新启动我的应用程序吗?

1 个答案:

答案 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运行相同的测试并看到相同的结果。