Timer.Elapsed属性在哪里?

8
我已经包含了System.Timers包,但是当我输入:
Timer.Elapsed; //its not working, the property elapsed is just not there.

我记得在VB.NET中有这个功能。为什么这个不起作用呢?

你想要做什么?获取自计时器启动以来流逝的时间吗? - Dave Zych
http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed.aspx - iCantSeeSharp
4个回答

38

它不是一个属性。 它是一个事件

因此,您需要提供一个事件处理程序,每次计时器滴答时都会执行。类似以下内容:

public void CreateTimer() 
{
    var timer = new System.Timers.Timer(1000); // fire every 1 second
    timer.Elapsed += HandleTimerElapsed;
}

public void HandleTimerElapsed(object sender, ElapsedEventArgs e)
{
    // do whatever it is that you need to do on a timer
}

2
因为你的答案更短,更清晰,更直接,所以我点了赞。 - sergserg
我看到了 System.Timers.ElapsedEventArgs e,但是,你怎么将计时器添加到表单中呢?我尝试使用 this.Controls.add(timer),但是出现了错误,因为它需要 System.Windows.Form.Control - barlop
你能解释一下,什么时候使用 += new ElapsedEventHandler(MyHandler),什么时候只用 += MyHandler - Vassilis
@VassilisGr,要将事件处理程序与某个东西关联起来,可以使用+=;您还可以使用-=语法来删除事件处理程序。 - Downhillski
那么第四行代码具体是做什么的?它每秒钟调用右侧的函数吗? - Cornelius

6

微软公司的例子。http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed.aspx

Elapsed 是一个事件,因此需要一个事件处理程序。

using System;
using System.Timers;

public class Timer1
{
private static System.Timers.Timer aTimer;

public static void Main()
{       
    // Create a timer with a ten second interval.
    aTimer = new System.Timers.Timer(10000);

    // Hook up the Elapsed event for the timer.
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    // Set the Interval to 2 seconds (2000 milliseconds).
    aTimer.Interval = 2000;
    aTimer.Enabled = true;

    Console.WriteLine("Press the Enter key to exit the program.");
    Console.ReadLine();       
}

// Specify what you want to happen when the Elapsed event is  
// raised. 
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}

/* This code example produces output similar to the following:

Press the Enter key to exit the program.
The Elapsed event was raised at 5/20/2007 8:42:27 PM
The Elapsed event was raised at 5/20/2007 8:42:29 PM
The Elapsed event was raised at 5/20/2007 8:42:31 PM
...
 */

我在微软数据库中看到了这个,但我认为它具有与“private void timer1_Tick(object sender, EventArgs e)”相同的功能。但我想我解决了我的问题。谢谢大家的回答。 - ISeeSounds

3
这里之前的回答都是正确的,但是随着 .net 6 / VS2022 的发布,空值性变得非常重要,上述所有的回答都会引发编译器警告 CS8622。
你的解决方案会因应用程序而异,但一个安全的通用解决方案是在回调函数的参数中将源对象标记为可为空,就像这样:
...
    var timer = new System.Timers.Timer(2000); // every 2000ms
    timer.Elapsed += TimerElapsedHandler;
...

public void TimerElapsedHandler(object? source, ElapsedEventArgs e)
{
    //Your Handling Code Here
}

如果你因为任何原因需要访问“source”,请确保首先检查它是否为空。

0

你需要一个事件处理程序,然后在分配事件处理程序时启用并在处理程序中停止一个条件。

 Timer = new System.Timers.Timer();
 Timer.Elapsed += new System.Timers.ElapsedEventHandler(PageLoaded);
 Timer.Interval = 3000;
 Timer.Enabled = true;

...................

 public void PageLoaded(object source, System.Timers.ElapsedEventArgs e)
        {
            // Do what ever here
            if (StopCondition)Timer.Enabled = false;
          
           
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接