C# Timer.Change 方法 [英] C# Timer.Change Method

查看:109
本文介绍了C# Timer.Change 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论结果如何,我都需要创建一个每 5 秒运行一次的函数,我浏览了 MSDN 库并发现了这个 http://msdn.microsoft.com/en-us/library/yz1c7148.aspx ,我认为这是我需要的,但我需要详细了解到期时间和期限参数

I am required to create a function that will run every 5 second no matter what is the outcome , I go through the MSDN library and found out this http://msdn.microsoft.com/en-us/library/yz1c7148.aspx , I think this is what I need , but I need to know more about the due time and period parameters

据我所知:-DueTime 是在启动计时器之前等待的时间吗?Periodtime是重复时间的时间吗?

For my understanding:- DueTime is it the time that will wait before start the timer ? Periodtime is it time that repeat the time ?

我的问题是我对 msdn 提供的 Timer 的理解是否正确.此外,如果我想设置我的时间每 5 秒运行一次,无论前一个计时器是否完成,我应该如何设置?

my question is whether my understanding is correct regarding the Timer provide by msdn. besides , if I want to set my time runn every 5 second no matter the previous timer is complete or not what should I set it ?

推荐答案

你的理解完全正确.下面的代码应该做你想做的:

You are absolutely correct in your understanding. The following code should do what you want:

System.Threading.Timer timer = new System.Threading.Timer(Callback, null, 0, 5 * 1000);

但是我应该提到 System.Threading.Timer 不太准确,每次调用都会有一些累积错误.例如下面的代码

however I should mention what System.Threading.Timer is not quite accurate and there will be some accumulating error on each call. For example the following code

static void Main(string[] args)
{
    System.Threading.Timer timer = new System.Threading.Timer(Callback, null, 0, 1 * 1000);
    Console.ReadLine();
}

static void Callback(object state)
{
    Console.WriteLine(DateTime.Now.ToString("hh:MM:ss:ffff"));
}

在我的电脑上产生以下结果:

produces the following result on my computer:

03:10:14:8014
03:10:15:8154
03:10:16:8294
03:10:17:8434
03:10:18:8574
03:10:19:8714
03:10:20:8854
03:10:21:8994
03:10:22:9134
03:10:23:9274
03:10:24:9414
03:10:25:9554
03:10:26:9694
03:10:27:9834
03:10:28:9974
03:10:30:0114
03:10:31:0254

这篇关于C# Timer.Change 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆