好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解析.NET中几种Timer的使用

这篇博客将梳理一下.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。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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内时间,

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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上的时间,代码如下,

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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类似。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

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

原文链接:http://www.cnblogs.com/yang-fei/p/6169089.html

dy("nrwz");

查看更多关于解析.NET中几种Timer的使用的详细内容...

  阅读:75次