当前位置: 移动技术网 > IT编程>开发语言>c# > 解析.NET中几种Timer的使用

解析.NET中几种Timer的使用

2019年07月18日  | 移动技术网IT编程  | 我要评论

这篇博客将梳理一下.net中4个timer类,及其用法。

1. system.threading.timer

public timer(timercallback callback, object state, int duetime, int period);

callback委托将会在period时间间隔内重复执行,state参数可以传入想在callback委托中处理的对象,duetime标识多久后callback开始执行,period标识多久执行一次callback。

using system.threading;
// system.threading.timer
timer timer = new timer(delegate
{
 console.writeline($"timer thread: {thread.currentthread.managedthreadid}");
 console.writeline($"is thread pool: {thread.currentthread.isthreadpoolthread}");
 console.writeline("timer action.");
},
null,
2000,
);
console.writeline("main action.");
console.writeline($"main thread: {thread.currentthread.managedthreadid}");
console.readline();

timer回掉方法执行是在另外threadpool中一条新线程中执行的。

2. system.timers.timer

system.timers.timer和system.threading.timer相比,提供了更多的属性,

interval  指定执行elapsed事件的时间间隔;

elapsed  指定定期执行的事件;

enabled  用于start/stop timer;

start    开启timer

stop    停止timer

system.timers.timer timer = new system.timers.timer();
timer.interval = 500;
timer.elapsed += delegate
{
 console.writeline($"timer thread: {thread.currentthread.managedthreadid}");
 console.writeline($"is thread pool: {thread.currentthread.isthreadpoolthread}");
 console.writeline("timer action");
 timer.stop();
};
timer.start();
console.writeline("main action.");
console.writeline($"main thread: {thread.currentthread.managedthreadid}");
console.readline();

timer elapsed定期任务是在threadpool的线程中执行的。

3. system.windows.forms.timer

interval  指定执行elapsed事件的时间间隔;

tick       指定定期执行的事件;

enabled  用于start/stop timer;

start    开启timer

stop    停止timer

使用system.windows.forms.timer来更新窗体中label内时间,

using system.windows.forms;
public form1()
{
 initializecomponent();
 this.load += delegate
 {
 timer timer = new timer();
 timer.interval = 500;
 timer.tick += delegate
 {
 system.diagnostics.debug.writeline($"timer thread: {system.threading.thread.currentthread.managedthreadid}");
 system.diagnostics.debug.writeline($"is thread pool: {system.threading.thread.currentthread.isthreadpoolthread}");
 this.lbltimer.text = datetime.now.tolongtimestring();
 };
 timer.start();
 system.diagnostics.debug.writeline($"main thread: {system.threading.thread.currentthread.managedthreadid}");
 };
}

timer tick事件中执行的事件线程与主窗体的线程是同一个,并没有创建新线程(或者使用threadpool中线程)来更新ui。下面将代码做一个改动,使用system.timers.timer来更新ui上的时间,代码如下,

public form1()
{
 initializecomponent();
 this.load += delegate
 {
 system.timers.timer timer = new system.timers.timer();
 timer.interval = 500;
 timer.elapsed += delegate
 {
 system.diagnostics.debug.writeline($"timer thread: {system.threading.thread.currentthread.managedthreadid}");
 system.diagnostics.debug.writeline($"is thread pool: {system.threading.thread.currentthread.isthreadpoolthread}");
 this.lbltimer.text = datetime.now.tolongtimestring();
 };
 timer.start();
 system.diagnostics.debug.writeline($"main thread: {system.threading.thread.currentthread.managedthreadid}");
 };
}

很熟悉的一个错误。因为label是由ui线程创建的,所以对其进行修改需要在ui线程中进行。system.timers.timer中elasped执行是在threadpool中新创建的线程中执行的。所以会有上面的错误。

4. system.windows.threading.dispatchertimer

属性和方法与system.windows.forms.timer类似。

using system.windows.threading;
public mainwindow()
{
 initializecomponent();
 this.loaded += delegate
 {
 //dispatchertimer
 dispatchertimer timer = new dispatchertimer();
 timer.interval = timespan.fromseconds(1);
 timer.start();
 debug.writeline($"main thread id: {thread.currentthread.managedthreadid}");
 timer.tick += delegate
 {
 tbtime.text = datetime.now.tolongtimestring();
 debug.writeline($"timer thread id: {thread.currentthread.managedthreadid}");
 timer.stop();
 };
 };
}

dispatchertimer中tick事件执行是在主线程中进行的。

使用dispatchertimer时有一点需要注意,因为dispatchertimer的tick事件是排在dispatcher队列中的,当系统在高负荷时,不能保证在interval时间段执行,可能会有轻微的延迟,但是绝对可以保证tick的执行不会早于interval设置的时间。如果对tick执行时间准确性高可以设置dispatchertimer的priority。例如:

dispatchertimer timer = new dispatchertimer(dispatcherpriority.send);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网