views:

88

answers:

1

When doing a begin... async call, the delegate I pass is handled (according to the documentation) in the default threadpool.

for instance: System.IO.Stream.BeginRead( byte[] buffer, int offset, int count, AsyncCallback callback, object state);

How can I make it so that I can use a dedicated threadpool for async method handling?

(I know this can be done since the CCR (Concurrency Coordination Runtime) is also doing this (according to their documentation))

+1  A: 

Strictly speaking it is handled in the IO part of the thread pool (asynchronous IO operations have their own set of threads separate from those used by ThreadPool.Queue* methods).

About the only way to do this currently (with released/supported tools) would be to have a stub method passed to BeginRead that forwards execution to your own thread pool:

var async = stream.BeginRead(buffer, offset, count,
                            ayn => { MyThreadPool.Dispatch(() => {
                              // Handle completion
                            }}, null);

The Reactive Framework Extensions (RX) would make this easier: create an IScheduler implementation for your threadpool, but RX is a CTP and likely to be a while before any form of go live.

Richard
so as I understand you correctly: the .Net threadpool is used for a split second to pass the call through?
Toad
Yes. I don't think it is possible to avoid this because the asynchronous IO uses the .NET FX IO Completion port that is server by the IO threadpool.
Richard