简体   繁体   English

如何从此计时器的回调 function 更改 System.Threading.Timer 中的间隔时间?

[英]How do I change the interval time in System.Threading.Timer from the callback function of this timer?

How do I change the interval in System.Threading.Timer from the callback function of this timer?如何从此计时器的回调 function 更改 System.Threading.Timer 中的间隔? Is this correct?这个对吗?

Doing so.这样做。 Did not happen.没有发生。

public class TestTimer
{
    private static Timer _timer = new Timer(TimerCallBack); 

    public void Run()
    {
        _timer.Change(TimeSpan.Zero, TimeSpan.FromMinutes(1));
    }

    private static void TimerCallBack(object obj)
    {
        if(true)
            _timer.Change(TimeSpan.Zero, TimeSpan.FromMinutes(10));
    }

}

This line generate infinite recursion:此行生成无限递归:

if(true)
    _timer.Change(TimeSpan.Zero, TimeSpan.FromMinutes(10));

The first parameter forces TimerCallBack to execute right away.第一个参数强制TimerCallBack立即执行。 So it executes it again and again indefinitely.所以它会无限期地一次又一次地执行它。

The fix would be修复将是

if(true)
    _timer.Change(TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10));

The problem is that your call to Change specifies that the next call should happen immediately .问题是您对Change的调用指定下一次调用应该立即发生。 If you're going to call Change every time, you can just use a period of Timeout.Infinite (which is just a constant of -1) to tell it to avoid repeating at all after the next time - but it will still keep firing, because that next time, you reset it.如果你要每次都调用Change ,你可以使用Timeout.Infinite的一段时间(它只是一个常数 -1)来告诉它在下一次之后完全避免重复 - 但它仍然会继续触发,因为下次你重置它。 For example:例如:

using System;
using System.Threading;

static class Program
{
    private static Timer timer = new Timer(TimerCallBack); 

    public static void Main()
    {
        timer.Change(TimeSpan.Zero, TimeSpan.FromSeconds(1));
        Thread.Sleep(10000);

    }

    private static void TimerCallBack(object obj)
    {
        Console.WriteLine("{0}: Fired", DateTime.Now);
        timer.Change(TimeSpan.FromSeconds(3),
                     TimeSpan.FromMilliseconds(Timeout.Infinite));
    }
}

Alternatively, you could change it just once, and then leave it:或者,您可以只更改一次,然后离开它:

using System;
using System.Threading;

static class Program
{
    private static Timer timer = new Timer(TimerCallBack); 
    private static bool changed = false;

    public static void Main()
    {
        timer.Change(TimeSpan.Zero, TimeSpan.FromSeconds(1));
        Thread.Sleep(10000);

    }

    private static void TimerCallBack(object obj)
    {
        Console.WriteLine("{0}: Fired", DateTime.Now);
        if (!changed)
        {
            changed = true;
            TimeSpan interval = TimeSpan.FromSeconds(3);
            timer.Change(interval, interval);
        }
    }
}

Note that nothing is using the initial interval (1 second in the samples above) in either case, because we're calling Change immediately - if you really want a different time before the first call, don't use TimeSpan.Zero in the initial call to Change .请注意,在任何一种情况下都没有使用初始间隔(上面的示例中为 1 秒),因为我们正在立即调用Change - 如果您真的想要在第一次调用之前有一个不同的时间,请不要在初始时使用TimeSpan.Zero调用Change

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM