Share via


Thread Pools Technology Sample

This sample demonstrates the use of the ThreadPool class. The sample queues up an asynchronous method call that is executed by a thread from the thread pool managed by the common language runtime.

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

To build the sample using the command prompt

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

  2. Type msbuild PoolsCS.sln or msbuild PoolsVB.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 PoolsCS.sln or PoolsVB.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 file, using the command prompt.

  2. Type AsyncMethod.exe at the command line.

    NoteNote

     This sample builds a console application. You must start it from the command prompt in order to view its output.

Remarks

This sample uses queuing to manage asynchronous method calls. The called method "does some work" and then sets an event indicating that the work has finished. The main thread waits on the event and then exits.

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

  • Threading

    • ThreadPool When writing managed code, it is suggested that whenever possible, developers use the QueueUserWorkItem method to implement asynchronous method calls. This sample demonstrates simple use of this type to execute asynchronous behavior.
  • Thread Synchronization

    • AutoResetEvent The sample's main thread waits on an AutoResetEvent object until it is set by the asynchronous method. The asynchronous method calls the Set method when it has finished with its work.
  • Delegates

    • WaitCallback Used to create a type-safe callback method for the ThreadPool class to use.

Review comments in the source code files for more information on thread synchronization.

See Also

Reference

AutoResetEvent
Delegate
System.Threading
ThreadPool
ThreadStart
WaitCallback

Concepts

Threads and Threading

Other Resources

Threading Objects and Features