다음을 통해 공유


Timers Technology Sample

This sample demonstrates the uses of timers in the common language runtime. The sample uses the Timer class to generate a periodic callback to a method. The sample creates a Timer and passes to it a delegate. When the Timer fires, the delegate is invoked, and a static method is called asynchronously by a worker thread in the thread pool.

For more information about using the samples, see the following topics:

To build the sample using the command prompt

  1. Open the Command Prompt window and navigate to one of the language-specific subdirectories for the sample.

  2. Type msbuild TimersCS.sln or msbuild TimersVB.sln, depending on your choice of programming language, at the command line.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories for the sample.

  2. Double-click the icon for TimersCS.sln or TimersVB.sln, depending on your choice of programming language, to open the file in Visual Studio.

  3. On the Build menu, click Build Solution.

To run the sample

  1. Navigate to the directory that contains the built executable, using the command prompt.

  2. Type TimedCallback.exe from the command line.

    NoteNote

    This sample builds a console application. You must launch it from a console window in order to view its output.

Remarks

The following bullets briefly describe the technologies and classes used by this sample.

  • Thread Timers

    • Timer Used to create periodic or singleton timers for relative or absolute times. The sample creates a periodic timer that invokes a delegate every two seconds until the application exits.
  • Thread Pools Although the ThreadPool class is not explicitly referenced by this sample, the Timer class uses the thread pool managed by the common language runtime to invoke delegates asynchronously in response to timer events. The timer in this sample uses a worker thread in the pool every two seconds.

  • Delegates

    • TimerCallback Used to create a typesafe callback method for the timer.

For more information about using timers with asynchronous method calls, see the comments in the source code files.

See Also

Reference

AutoResetEvent
Delegate
Interlocked
Mutex
System.Threading
ThreadPool
WaitCallback
WaitHandle
Timer

Concepts

Threads and Threading

Other Resources

Threading Objects and Features