timer计时 wpf_『WPF』Timer的使用

几点说明 由于正在做的一个小项目需要使用计时的功能,所以有了这个Timer使用的文章

这篇的内容主要参考自MSDN中有关Timer使用的说明

以后如果遇到更高层次的使用方法,会继续更新这篇文章

来自MSDN的说明System.Timers.Timer Name Description

Method Timer

「构造方法」 Initializes a new instance of the Timer class, and sets all the properties to their initial values.

初始化一个Timer类的新实例,以及设置初始值,精确度为秒。

Method Timer(Double)

「构造方法」 Initializes a new instance of the Timer class, and sets the Interval property to the specified number of milliseconds.

初始化一个Timer类的新实例,以及设置初始值,精确度为毫秒。

Property AutoReset

「自动重置」 Gets or sets a value indicating whether the Timer should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses.

获取或设置一个值指示 Timer 是「每次都调用Elapsed 事件」还是「只有第一次调用」。

Property SynchronizingObject

「同步对象」 Gets or sets the object used to marshal event-handler calls that are issued when an interval has elapsed.

获取或设置用于整理超过设置的时间间隔的事件调用。

Method Start

「Public」 Starts raising the Elapsed event by setting Enabled to true.

通过设置Enalbed为true,马上开始调用Elapsed

Method Stop

「Public」 Stops raising the Elapsed event by setting Enabled to false.

通过设置Enalbed为false,马上停止调用Elapsed

Event Elapsed

「流逝」 Occurs when the interval elapses.

当时间间隔「流逝」时触发。

The Timer component is a server-based timer, which allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing.

Timer组件是一个基于服务器的计时器,它允许你指定一个有间隔的「调用」Elapsed事件的循环。你可以捕获这个事件来进行你的处理。 Note: When AutoReset is set to false, the Timer raises the Elapsed event only once, after the first Interval has elapsed. To keep raising the Elapsed event on the Interval, set AutoReset to true.

当AutoReset属性设置为false时,Timer在第一次时间间隔到了之后,只调用一次Elapsed事件。为了保证每次时间间隔都调用Elapsed事件,要设置AutoReset属性为true If the SynchronizingObject property is Nothing, the Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. In this situation, the event handler should be reentrant.

如果SynchronizingObject 属性没有设置, Elapsed事件被一个线程池线程调用。如果Elapsed事件持续的时间比一次间隔要长,这个事件可能会在另一个线程池的线程被调用。在这种情况下,事件处理应该是可重复的。 Note: The event-handling method might run on one thread at the same time that another thread calls the Stop method or sets the Enabled property to false. This might result in the Elapsed event being raised after the timer is stopped.

当一个线程调用Stop方法或设置Enabled属性为false时,相应的事件处理方法可能会在不同的线程中继续执行。这可能导致Elapsed事件在计时器已经被停止的时候继续执行。 Even if SynchronizingObject is not Nothing, Elapsed events can occur after the Dispose or Stop method has been called or after the Enabled property has been set to false, because the signal to raise the Elapsed event is always queued for execution on a thread pool thread. One way to resolve this race condition is to set a flag that tells the event handler for the Elapsed event to ignore subsequent events.

即使SynchronizingObject 已经设置了,Elapsed事件还是会发生在Dispose或Stop方法被调用之后或在Enabled属性被设置成false后,因为调用Elapsed事件的「符号」总是在一个线程池中列队执行。一个解决这个竞争条件的方法是设置一个「标识」告诉Elapsed事件处理程序忽略掉随后的事件。

MSDN示例程序using System;

using System.Timers;

public class Timer1

{

private static System.Timers.Timer aTimer;

public static void Main()

{

// Normally, the timer is declared at the class level,

// so that it stays in scope as long as it is needed.

// If the timer is declared in a long-running method,

// KeepAlive must be used to prevent the JIT compiler

// from allowing aggressive garbage collection to occur

// before the method ends. You can experiment with this

// by commenting out the class-level declaration and

// uncommenting the declaration below; then uncomment

// the GC.KeepAlive(aTimer) at the end of the method.

//System.Timers.Timer aTimer;

// 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();

// If the timer is declared in a long-running method, use

// KeepAlive to prevent garbage collection from occurring

// before the method ends.

//GC.KeepAlive(aTimer);

}

// 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

...

*/

个人演示程序

TimerWindow

TimerWindow的XAML内容

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Timer测试 - http://www.cnblogs.com/sitemanager/" Height="89" Width="287" Loaded="Window_Loaded">

TimerWindow的后台代码using System.Windows;

using System.Timers;

namespace csdemo.wpf.controls.Timer

{

///

/// TimerWindow.xaml 的交互逻辑

///

public partial class TimerWindow : Window

{

public TimerWindow()

{

InitializeComponent();

}

private void btnStart_Click(object sender, RoutedEventArgs e)

{

aTimer.Start();

}

System.Timers.Timer aTimer = new System.Timers.Timer();

static int elapsedTimes;

private void Window_Loaded(object sender, RoutedEventArgs e)

{

aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

aTimer.Interval = 2000; // 1秒 = 1000毫秒

}

///

/// Timer的Elapsed事件处理程序

///

///

///

private static void OnTimedEvent(object source, ElapsedEventArgs e)

{

MessageBox.Show((++elapsedTimes).ToString(), "Timer测试 - http://www.cnblogs.com/sitemanager/");

}

private void btnStop_Click(object sender, RoutedEventArgs e)

{

aTimer.Stop();

MessageBox.Show("Timer已停止,之前共触发次" + (elapsedTimes).ToString() + "事件", "Timer测试 - http://www.cnblogs.com/sitemanager/");

elapsedTimes = 0;

}

private void btnClose_Click(object sender, RoutedEventArgs e)

{

aTimer.Dispose(); // 清理aTimer使用的内存

MessageBox.Show("欢迎使用『峻之岭峰』的WPF控件Demo,您可以在我的博客中看到最新发表的有关编程技术的个人总结。 \n 博客地址: http://www.cnblogs.com/sitemanager/ \n\n如果您是在点击停止之前点击此按钮,将会造成无法停止Timer!\n此时您可以返回您的开发工具停止调试项目。\n或直接在资源管理器中终止进程。\n\n\n 在使用本Demo的同时,由于个人的开发环境不同,所以请不要简单的拷贝代码", "『峻之岭峰』的WPF控件Demo - http://www.cnblogs.com/sitemanager/");

this.Close();

}

}

}

说明:注意之前说过的Timer Elapsed事件的线程有关的问题

注意,你可以在任何你想开始计时的地方调用Timer的Start()方法开始计时,调用Stop()方法停止计时。

运行效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值