■ TaskFactory 클래스 : FromAsync 메소드를 사용해 비동기 프로그래밍 모델 실행하기

------------------------------------------------------------------------------------------------------------------------

Program.cs

 

 

using System;

using System.IO;

using System.Threading.Tasks;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 완료 여부

        /// </summary>

        private static bool _isCompleted;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 파일 읽기 (비동기) - ReadFileAsync(filePath)

 

        /// <summary>

        /// 파일 읽기 (비동기)

        /// </summary>

        /// <param name="filePath">파일 경로</param>

        /// <returns>태스크</returns>

        private static Task<int> ReadFileAsync(string filePath)

        {

            FileStream fileStream = File.OpenRead(filePath);

 

            byte[] bufferByteArray = new Byte[fileStream.Length];

 

            Task<int> task = Task.Factory.FromAsync

            (

                (Func<byte[], int, int, AsyncCallback, object, IAsyncResult>)fileStream.BeginRead,

                (Func<IAsyncResult, int>)fileStream.EndRead,

                bufferByteArray,

                0,

                (int)fileStream.Length,

                null

            );

 

            task.ContinueWith

            (

                task =>

                {

                    if(task.Status == TaskStatus.RanToCompletion)

                    {

                        _isCompleted = true;

 

                        Console.WriteLine("파일명 : {0}, 바이트 수 : {1}", filePath, task.Result);

                    }

 

                    fileStream.Dispose();

                }

            );

 

            return task;

        }

 

        #endregion    

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            _isCompleted = false;

 

            ReadFileAsync("d:\\source.txt");

 

            int i = 0;

 

            do

            {

                Console.WriteLine("타이머 카운터 : {0}", ++i);

            }

            while(!_isCompleted);

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요