Ziff-Davis Enterprise 
DevSource: Microsoft Developer Resource
Add OnsArchitectureLanguagesTechniquesUsing VSForums
 
Home arrow Using VS arrow Page 4 - Appreciating the System ThreadPool
Appreciating the System ThreadPool
By Jon Shemitz

Rate This Article:
Add This Article To:
Appreciating the System ThreadPool - ' Wait Callbacks '
( Page 4 of 4 )

Wait Callbacks

Many tasks that take significant clock time actually take comparatively little CPU time, because they spend most of their time blocked, waiting for an OS wait handle. They may be waiting for network activity, file IO, a timer, or whatever.

ADVERTISEMENT

As an alternative to creating a thread that spends a lot of time blocked, waiting for one event or another, you can use ThreadPool. RegisterWaitForSingleObject to register an OS wait handle and a WaitOrTimerCallback delegate that gets called when the handle is signaled. The ThreadPool uses OS functions that block a single thread until any of several wait handles is signaled. This thread determines which wait handle signaled, then calls the associated delegate in a worker thread.

This not only avoids thread creation overhead but also reduces the number of blocked threads. For example, it may be easier, clearer, and safer to implement a network protocol as a thread which blocks at each step of the protocol; but it may be more efficient to implement the protocol as a web of delegates called as each handle is signaled, most of which fire off some response and then register another delegate and another wait. (The safest way to create such a web is probably to compile some sort of blocking script.)

Each wait can timeout or the wait can be infinite; the WaitOrTimerCallback delegate takes a parameter that tells the delegate whether it is being called because the handle was signaled or because the wait timed out.

Registering a wait with the ThreadPool is perhaps most appropriate where the event will keep being signaled, and you want to do the same thing each time. (You may be monitoring some sort of news feed, for example, or trading messages with another process.) Each call to ThreadPool.RegisterWaitForSingleObject includes an executeOnlyOnce parameter; when the Only Once parameter is false, the ThreadPool will keep waiting for the registered event until you call Unregister on the RegisteredWaitHandle you get from RegisterWaitForSingleObject.

Jon Shemitz is a consultant and an author. You may contact him at www.midnightbeach.com. This article will appear, in different form, in his forthcoming book, .NET 2.0 For Delphi Programmers (Apress, June 2006).



 
 
>>> More Using VS Articles          >>> More By Jon Shemitz
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog

Julia explores the Robotics Studio! (It's for more than you think.)

MSDev Blog

Messages for Bill Gates!

Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.