Asynchronous Streams?
-
I've been playing with asynchronous streams and have a question. When I use the beginRead method for asynchronous reading, the for loop in the code below never gets called until the entire file(fairly large) is completely read(ie. mutiple begin reads and callbacks). I thought that the code should process the for loop while doing some reading of the file and when the read completes right it to the screen. This continues until the entire file is read thereby alternating the output from the for loop and the reading of the file. This is not occuring for me. Is there something I'm missing? I've tried this numerous times using different types of streams and the same thing occurs. Code snippet below: FileStream inputStream; // inputStream is a filestream public void Run() { inputStream.BeginRead(buffer,0,buffer.Length,new AsyncCallback(this.Callback),null); for(long i = 0; i <500000; i++) { if(i%10000 ==0) Console.WriteLine(i.ToString()); } } public void Callback(IAsyncResult ar) { int bytesRead = inputStream.EndRead(ar); // write output to screen }