Pools example

This sample demonstrates use of the THREAD_POOL (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. The 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.

 

Compiling

Running

After you launch the sample, the following output appears:

Main thread: Queuing an asynchronous operation.
Main thread: Performing other operations.
WorkItem thread: Performing asynchronous operation
Main thread: Waiting for asynchronous operation to complete.

When the display is finished, the application wait for you to pressed the return key to finished the application.

 

Under the Hood

This application shows how to use the thread THREAD_POOL. An asynchronous thread is launched (feature {THREAD_POOL}.queue_user_work_item_wait_callback_object (create {WAIT_CALLBACK}.make (Current, $my_async_operation), l_async_operation_done)) and is associated to the local variable l_async_operation_done. Both threads perform simultaneously some operations. The main thread wait for the asynchronous thread to complete his own operations (l_async_operation_done.wait_one) to close the application.

This sample contains the following class:

 

Notes

This sample is translated from the example located in the Samples\Technologies\Threading\Pools subdirectory of the .NET Framework SDK samples directory of Microsoft Visual Studio .NET.