Asynchronous IO with AsyncCallback : Asynchronous Input Output « File Directory Stream « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

   public class MainClass
   {
      static Stream inputStream;
      static AsyncCallback myCallBack;
      static byte[] buffer;
      static int BufferSize = 256;
      public static void Main()
      {
         inputStream = File.OpenRead("AskTim.txt" );
         buffer = new byte[BufferSize];
         myCallBack = new AsyncCallback( OnCompletedRead );
         inputStream.BeginRead(
            buffer,             // holds the results
            0,                  // offset
            buffer.Length,      // (BufferSize)
            myCallBack,         // call back delegate
            null );             // local state object
      }
      static void OnCompletedRead( IAsyncResult asyncResult )
      {
         int bytesRead = inputStream.EndRead( asyncResult );
         if ( bytesRead > 0 ){
            String s = Encoding.ASCII.GetString( buffer, 0, bytesRead );
            Console.WriteLine( s );
            inputStream.BeginRead(buffer, 0, buffer.Length, myCallBack, null );
         }
      }
   }








15.39.Asynchronous Input Output
15.39.1.Asynchronous I/O
15.39.2.Asynchronous I/O, some blocking on the main thread
15.39.3.Async File Stream
15.39.4.Asynchronous IO with AsyncCallback
15.39.5.Async FileStream demo