Asynchronous I/O : Asynchronous Input Output « File Directory Stream « C# / CSharp Tutorial






using System;
using System.IO;
using System.Windows.Forms;

class MainClass
{
  private static FileStream inStream;

  static void OnComplete(IAsyncResult asyncResult)
  {
    int bytesRead = inStream.EndRead(asyncResult);
    Console.Write(bytesRead);
  }

  public static void Main() 
  {
    inStream = new FileStream("C:\\text.txt", FileMode.Open, FileAccess.Read, FileShare.None, 2048, true);

    AsyncCallback  acb = new AsyncCallback(OnComplete);

    byte[] buf = new byte[50];
    
    inStream.BeginRead(buf, 0, 50, acb, null);

    for(int i=0; i<10; i++)
      Console.WriteLine(i);

    Console.WriteLine("Press Enter to exit");
    int resp = Console.Read();
  }
}








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