Facebook
From walsienacyce, 5 Years ago, written in C#.
This paste is a reply to chuj from chuj - go back
Embed
Viewing differences between chuj and Re: chuj
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace IO_lab03
{
    class Program
    {
        private static int temp;

        static void Main(string[] args)
        {
            string filePath = @"C:\Users\s0163726\source\repos\IO_lab03\IO_lab03\pliczek.@"C:Userss0163726sourcereposIO_lab03IO_lab03pliczek.txt";
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] data = new byte[1024];
            IAsyncResult ar = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });

            IAsyncResult ar1 = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });

            IAsyncResult ar2 = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });

            IAsyncResult ar3 = fs.BeginRead(data, 0, 1024, myAsyncCallback, new { Data = data, Stream = fs });
            
            WaitHandle.WaitAll()

            for (int i = 0; i < 100000; i++)
                for (int j = 0; j < 100000; j++)
                    temp = i * j;
        }

        private static void myAsyncCallback(IAsyncResult ar)
        {

            dynamic args = ar.AsyncState;
            byte[] data = args.Data;
            FileStream fs = args.Stream;
            int l = fs.EndRead(ar);
            Console.WriteLine(ASCIIEncoding.ASCII.GetString(data).Substring(0, l));

        }

        public static async void ProcessRead()
        {
            string filePath = @"C:\Users\s0163726\source\repos\IO_lab03\IO_lab03\pliczek.@"C:Userss0163726sourcereposIO_lab03IO_lab03pliczek.txt";
            if (File.Exists(filePath) == false)
            {
                Console.WriteLine("file not found: " + filePath);
            }
            else
            {
                try
                {
                    string text = await ReadTextAsync(filePath);
                    Console.WriteLine(text);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private static async Task ReadTextAsync(string filePath)
        {
            using (FileStream sourceStream = new FileStream(filePath,
                FileMode.Open, FileAccess.Read, FileShare.Read,
                bufferSize: 4096, useAsync: true))
            {
                StringBuilder sb = new StringBuilder();

                byte[] buffer = new byte[0x1000];
                int numRead;
                while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
                {
                    string text = Encoding.Unicode.GetString(buffer, 0, numRead);
                    sb.Append(text);
                }

                return sb.ToString();
            }
        }
    }
}


Replies to Re: chuj rss

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