2 Nov 2009, 2:48pm
Hi Rick - thanks for the great book. Quick question on the section regarding Asynchronous File I/O. In the example you give, the code reads 65536 bytes (64 *1024) from a specified file, storing this in this.Data. Say I would like to flush this buffer out to Response.OutputStream, and continue to read the rest of the file asynchronously, regularly flushing the bytes to the Response.
My goal would be to deliver an entire file asynchronously, without occupying asp.net request threads.
I could theoretically read the entire file into this.Data before writing, but this would not scale for large files -- I'm dealing with files that are 100 MB and more.
Any idea how I may accomplish this?
One approach would be to register the BeginAsync method again from the EndAsync method, using RegisterAsyncTask(). When you register tasks at that stage of the life cycle, you also need to call ExecuteRegisteredAsyncTasks() to start them. Then, just use a field in the page to track how many times the task needs to be called. The page won't complete until all async tasks are complete, including those that you register and start during async processing.
Depending on the details of your app, you might also consider doing this from a background thread, using a custom IAsyncResult. Serializing disk I/O can help improve performance by minimizing disk seeks.
--Rick