System.Threading命名空间的计时器类 [英] Timer Class of System.Threading name space

查看:96
本文介绍了System.Threading命名空间的计时器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我从书中获得的以下代码示例中,我需要帮助.在示例中,我试图使用System.Threadind命名空间的Timer类在第二秒后重复十分钟后调用方法TimedMethod().有人告诉我使用Timer类的Start()方法(在下面的代码中进行了注释),但是我收到一条错误消息,表明该类没有这种方法.我该如何解决这个问题?这里是什么问题?

I need help in the below code example that i got from a book.In the example iam trying to call a methode TimedMethod() after ten minutes repeatedly after a secod using the Timer class of System.Threadind name space. I was told to use the Start() method(commented in the following code) of the Timer class but i get an error indicating that the class has no such method. How can i achieve this or what can be the problem here?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ImplementingTimers
{
    class Program
    {
        static void TimedMethod(Object stateInfo)
        {
            Console.WriteLine("Executed");
        }

        static void Main(string[] args)
        {
            TimerCallback timerDelegate = new TimerCallback(TimedMethod);
            Timer stateTimer = new Timer(timerDelegate, null, 10000, 1000);
            //stateTimer.Start();
            
        }
    }
}

推荐答案

这不是 ^ ]有效...

当您创建计时器时,您可以指定在第一次执行该方法之前要等待的时间(到期时间),以及在随后的两次执行之间要等待的时间(周期).您可以更改这些值,或使用Change方法禁用计时器." -MSDN.

您确定这本书的作者没有使用其他两个计时器之一 [ ^ ]? System.Timers.Timer [System.Windows.Forms.Timer [
That is not how the System.Threading.Timer[^] works...

"When you create a timer, you can specify an amount of time to wait before the first execution of the method (due time), and an amount of time to wait between subsequent executions (period). You can change these values, or disable the timer, using the Change method." -MSDN.

Are you sure the author of the book is not using one of the other two Timers[^]? A System.Timers.Timer[^] or System.Windows.Forms.Timer[^]?
These two DO have a Start Method (although they don''t have a constructor that takes a delegate as parameter)... :)
Hope that helps.


这篇关于System.Threading命名空间的计时器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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