C# asynchronous IO demo code

From , 2 Years ago, written in C#, viewed 200 times.
URL https://pastebin.vip/view/33cc2b87
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4.  
  5. public class Example15_18
  6. {
  7.   // stream to handle reading
  8.   private static FileStream inStream;
  9.  
  10.   // delegated method to handle callback
  11.   private static AsyncCallback acb;
  12.  
  13.   // allocate a big buffer for reading
  14.   static byte[] buf = new byte[500000];
  15.  
  16.   // callback to use when read is complete
  17.   static void OnComplete(IAsyncResult asyncResult)
  18.   {
  19.     int bytesRead = inStream.EndRead(asyncResult);
  20.     Console.Write(bytesRead);
  21.     Console.WriteLine(" bytes read!");
  22.   }
  23.  
  24.     [STAThread]
  25.   public static void Main()
  26.   {
  27.     // use an open file dialog to get a filename
  28.     OpenFileDialog dlgOpen = new OpenFileDialog();
  29.     dlgOpen.Title="Select file to read";
  30.  
  31.     if (dlgOpen.ShowDialog() == DialogResult.OK)
  32.     {
  33.       // open the file
  34.       inStream = new FileStream(dlgOpen.FileName, FileMode.Open,
  35.        FileAccess.Read, FileShare.None, 2048, true);
  36.  
  37.       // assign the callback delegate
  38.       acb = new AsyncCallback(OnComplete);
  39.  
  40.       // read asynchronously
  41.       inStream.BeginRead(buf, 0, 500000, acb, null);
  42.  
  43.       // do some work in the meantime
  44.       for(int i=0; i<10; i++)
  45.         Console.WriteLine(i);
  46.       // And wait for the user to quit
  47.       Console.WriteLine("Press Enter to exit");
  48.       int resp = Console.Read();
  49.     }
  50.  
  51.   }
  52.  
  53. }
  54. //csharp/7645

Reply to "C# asynchronous IO demo code"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website