상세 컨텐츠

본문 제목

FileStream 비동기 입출력

C#

by 탑~! 2018. 6. 29. 15:38

본문

using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {
        static FileStream asyncfs =
                new FileStream("ASyncTest.txt", FileMode.Open, FileAccess.Read, FileShare.None, 255, true);        
        static AsyncCallback callback = new AsyncCallback(CallBackFunction);
        static byte[] readBuf = new byte[asyncfs.Length];

        static void Main(string[] args)
        {
            asyncfs.BeginRead(readBuf, 0, readBuf.Length, callback, null);

            forint i=0; i<3; i++ )
            {
                Console.WriteLine("This is a Main() Loop Code - " + (i+1).ToString());
                Thread.Sleep(1000);
            }

            asyncfs.Close();

        }

        static void CallBackFunction(IAsyncResult asyncResult)
        {
            int readB = asyncfs.EndRead(asyncResult);
            if( readB > 0 )
            {
                asyncfs.Seek(0, SeekOrigin.Begin);
                asyncfs.BeginRead(readBuf, 0, readBuf.Length, callback, null);
                Console.WriteLine(Encoding.ASCII.GetString(readBuf, 0, readB));
                Thread.Sleep(1000);
            }
        }
    }
}


'C#' 카테고리의 다른 글

웹 크롤링  (0) 2018.06.29
yield  (0) 2018.06.29
대용량 txt 파일 짜르기  (0) 2018.06.29
ENCODING / BITCONVERTER  (0) 2018.06.29
BinaryFormatter / XmlSerializer / DataContractJsonSerializer  (0) 2018.06.29

관련글 더보기