■ Task 클래스 : 스로틀링(Throttling) 처리하기

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


TestProject.zip


Program.cs

 

 

using System;

using System.Linq;

using System.Net.Http;

using System.Threading;

using System.Threading.Tasks;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

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

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

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

 

        #region Field

 

        /// <summary>

        /// 최대 다운로드 카운트

        /// </summary>

        private const int MAXIMUM_DOWNLOAD_COUNT = 3;

 

        #endregion

 

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

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

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

 

        #region 다운로드 하기 (비동기) - DownloadAsync(urlArray)

 

        /// <summary>

        /// 다운로드 하기 (비동기)

        /// </summary>

        /// <param name="urlArray">URL 배열</param>

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

        private static async Task DownloadAsync(string[] urlArray)

        {

            using(SemaphoreSlim semaphoreSlim = new SemaphoreSlim(MAXIMUM_DOWNLOAD_COUNT))

            {

                using(HttpClient httpClient = new HttpClient())

                {

                    var taskEnumerable = urlArray.Select

                    (

                        async url => {

 

                            await semaphoreSlim.WaitAsync();

 

                            try

                            {

                                string html = await httpClient.GetStringAsync(url);

 

                                Console.WriteLine(html);

                            }

                            finally

                            {

                                semaphoreSlim.Release();

                            }

                        }

                    );

 

                    await Task.WhenAll(taskEnumerable);

                }

            }

        }

 

        #endregion

        #region 다운로드 테스트 하기 - TestDownload()

 

        /// <summary>

        /// 다운로드 테스트 하기

        /// </summary>

        private async static void TestDownload()

        {

            await DownloadAsync

            (

                new string[]

                {

                    "http://www.daum.net",

                    "http://www.naver.com",

                    "http://www.daum.net",

                    "http://www.naver.com",

                    "http://www.daum.net",

                    "http://www.naver.com",

                    "http://www.daum.net",

                    "http://www.naver.com",

                    "http://www.daum.net",

                    "http://www.naver.com"

                }

            );

        }

 

        #endregion

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

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            TestDownload();

 

            Console.ReadKey();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요