简体   繁体   English

C# 定时器类 Change(dueDate, period)

[英]C# Timer class Change(dueDate, period)

i have go through the msdn library about this timer class change function ,我已经浏览了有关此计时器类更改功能的 msdn 库,

http://msdn.microsoft.com/en-us/library/yz1c7148.aspx http://msdn.microsoft.com/en-us/library/yz1c7148.aspx

public bool Change( int dueTime, int period ) public bool Change( int dueTime, int period )

But i do not understand what is the period parameter for.但我不明白周期参数是什么。

i also try to create a sample to see what it for but seems like it is doing nothing我也尝试创建一个示例来查看它的用途,但似乎它什么也没做

Timer JobTime = new Timer(timer =>
        {
            try
            {
                WriteLog(DateTime.Now.ToString(), "TestJobTimer"); //Save invoke time to file

                ((Timer)timer).Change(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20));
            }
            catch(Exception ex)
            {
                string stop = ex.Message;
            }
        });
        JobTime.Change(0, 0);

Base on this sample , what i get is the timer will repeat every 5 second , thus what is the PERIOD paramenter for ?基于这个示例,我得到的是计时器将每 5 秒重复一次,那么 PERIOD 参数是什么?

Thank you谢谢

dueTime shows when the first event will be fired, dueTime显示何时触发第一个事件,

period how often after that在那之后多久

in your case the first event will be fired after 5 second then after every 20 seconds在您的情况下,第一个事件将在 5 秒后触发,然后每 20 秒触发一次

EDIT编辑

As far as you are calling your timer change with 0,0, it starts impatiently and on timer tick call you change it to fire after 5 seconds every 20 second, that's why event fires every 5 seconds至于你用 0,0 调用你的计时器更改,它会不耐烦地开始并且在计时器滴答时调用你将它更改为每 20 秒 5 秒后触发,这就是事件每 5 秒触发一次的原因

If you want to fire event every 20 seconds after 5 seconds, remove timer change from handler, and start timer only once like this如果您想在 5 秒后每 20 秒触发一次事件,请从处理程序中删除计时器更改,并像这样只启动一次计时器

    Timer JobTime = new Timer(timer =>
    {
        try
        {
            Console.WriteLine(DateTime.Now.ToString(), "TestJobTimer"); //Save invoke time to file
        }
        catch (Exception ex)
        {
            string stop = ex.Message;
        }
    });
    JobTime.Change(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(20));

Hope this helps希望这可以帮助

DueTime = time until first deployment DueTime = 距离首次部署的时间

Period = amount of time after due time for next deployment, and amount of time for each subsequent deployment.周期 = 下一次部署的到期时间之后的时间量,以及每次后续部署的时间量。

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

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