Facebook
From walsienacyce, 5 Years ago, written in C#.
This paste is a reply to chuj from chuj - view diff
Embed
Download Paste or View Raw
Hits: 377
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace IO_lab03
  10. {
  11.     class Program
  12.     {
  13.         private static int temp;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             string filePath = @"C:Userss0163726sourcereposIO_lab03IO_lab03pliczek.txt";
  18.             FileStream fs = new FileStream(filePath, FileMode.Open);
  19.             byte[] data = new byte[1024];
  20.             IAsyncResult ar = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });
  21.  
  22.             IAsyncResult ar1 = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });
  23.  
  24.             IAsyncResult ar2 = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });
  25.  
  26.             IAsyncResult ar3 = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });
  27.            
  28.             WaitHandle.WaitAll()
  29.  
  30.             for (int i = 0; i < 100000; i++)
  31.                 for (int j = 0; j < 100000; j++)
  32.                     temp = i * j;
  33.         }
  34.  
  35.         private static void myAsyncCallback(IAsyncResult ar)
  36.         {
  37.  
  38.             dynamic args = ar.AsyncState;
  39.             byte[] data = args.Data;
  40.             FileStream fs = args.Stream;
  41.             int l = fs.EndRead(ar);
  42.             Console.WriteLine(ASCIIEncoding.ASCII.GetString(data).Substring(0, l));
  43.  
  44.         }
  45.  
  46.         public static async void ProcessRead()
  47.         {
  48.             string filePath = @"C:Userss0163726sourcereposIO_lab03IO_lab03pliczek.txt";
  49.             if (File.Exists(filePath) == false)
  50.             {
  51.                 Console.WriteLine("file not found: " + filePath);
  52.             }
  53.             else
  54.             {
  55.                 try
  56.                 {
  57.                     string text = await ReadTextAsync(filePath);
  58.                     Console.WriteLine(text);
  59.                 }
  60.                 catch (Exception ex)
  61.                 {
  62.                     Console.WriteLine(ex.Message);
  63.                 }
  64.             }
  65.         }
  66.  
  67.         private static async Task<string> ReadTextAsync(string filePath)
  68.         {
  69.             using (FileStream sourceStream = new FileStream(filePath,
  70.                 FileMode.Open, FileAccess.Read, FileShare.Read,
  71.                 bufferSize: 4096, useAsync: true))
  72.             {
  73.                 StringBuilder sb = new StringBuilder();
  74.  
  75.                 byte[] buffer = new byte[0x1000];
  76.                 int numRead;
  77.                 while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
  78.                 {
  79.                     string text = Encoding.Unicode.GetString(buffer, 0, numRead);
  80.                     sb.Append(text);
  81.                 }
  82.  
  83.                 return sb.ToString();
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89.  
  90.  

Replies to Re: chuj rss

Title Name Language When
Re: Re: chuj 44 csharp 5 Years ago.