Mega Code Archive

 
Categories / C# Tutorial / Thread
 

AsyncCallback and file viewer

using System; using System.IO; using System.Text; public class FileViewer {     FileStream license;     private AsyncCallback doneDelegate;     private byte[] buff = new byte[100];     public FileViewer()     {         license = new FileStream("C:\\your.txt", FileMode.Open);         doneDelegate = new AsyncCallback(UpdateConsole);         if (buff[0] > 0)         {             license.BeginRead(buff, 0, buff.Length, doneDelegate, this);         }     }     public void UpdateConsole(IAsyncResult result)     {         int totalBytes = license.EndRead(result);         if (totalBytes > 0)         {             String s = Encoding.ASCII.GetString(buff, 0, totalBytes);             Console.WriteLine(s);         }         if (buff[0] > 0)         {             license.BeginRead(buff, 0, buff.Length, doneDelegate, this);         }     } }