Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 22 INTRODUCTION TO ASYNCHRONOUS PROGRAMMING

Timers

Timers provide another way to run an asynchronous method on a regular, recurring basis. Although there are several Timer classes available in the .NET BCL, I’ll describe the one in the System.Threading namespace.

The important things to know about this timer class are the following:

The timer uses a callback method that is called each time the timer expires. The callback method must be in the form of the TimerCallback delegate, which has the following form. It takes a single parameter of type object and has a void return type.

void TimerCallback( object state )

When the timer expires, the system sets up the callback method on a thread from the thread pool, supplies the state object as its parameter, and starts it running.

You can set a number of the timer’s characteristics, including the following:

The dueTime is the amount of time before the first call of the callback method. If dueTime is set to the special value Timeout.Infinite, the timer will not start. If it’s set to 0, the callback is called immediately.

The period is the amount of time between each successive call of the callback method. If it’s value is set to Timeout.Infinite, the callback won’t be called after the first time.

The state is either null or a reference to an object to be passed to the callback method each time it’s executed.

The constructor for the Timer class takes as parameters the name of the callback method, the dueTime, the period, and the state. There are several constructors for Timer; the one that’s probably the most commonly used has the following form:

Timer( TimerCallback callback, object state, uint dueTime, uint period)

The following code statement shows an example of the creation of a Timer object:

Name of

Call first time after

the callback

2000 milliseconds

Timer myTimer = new Timer ( MyCallback, someObject, 2000, 1000 );

 

 

Object to pass

Call every

 

to the callback

1000 milliseconds

Once a Timer object is created, you can change its dueTime or period using the Change method.

624

CHAPTER 22 INTRODUCTION TO ASYNCHRONOUS PROGRAMMING

The following code shows an example of using a timer. The Main method creates the timer so that it will call the callback for the first time after two seconds and once every second after that. The callback method simply prints out a message, including the number of times it's been called.

using System;

using System.Threading;

namespace Timers

{

class Program

{

int TimesCalled = 0;

void Display (object state)

{

Console.WriteLine("{0} {1}",(string)state, ++TimesCalled);

}

 

static void Main( )

 

{

First callback at

Program p = new Program();

 

2 seconds

Timer myTimer = new Timer

(p.Display, "Processing timer event", 2000, 1000);

Console.WriteLine("Timer started.");

 

Repeat every

Console.ReadLine();

second

}

}

}

This code produces the following output before being terminated after about five seconds:

Timer started.

Processing timer event 1

Processing timer event 2

Processing timer event 3

Processing timer event 4

625

CHAPTER 22 INTRODUCTION TO ASYNCHRONOUS PROGRAMMING

There are several other timer classes supplied by the .NET BCL, each having its own uses. The other timer classes are the following:

System.Windows.Forms.Timer: This class is used in Windows Forms applications to periodically place WM_TIMER messages into the program’s message queue. When the program gets the message from the queue, it processes the handler synchronously on the main user interface thread. This is extremely important in Windows Forms applications.

System.Timers.Timer: This class is more extensive and contains a number of members for manipulating the timer through properties and methods. It also has a member event called Elapsed, which is raised when each period expires. This timer can run on either a user interface thread or a worker thread.

626

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]