본문 바로가기
컴퓨터/C#

Task.WaitAll 없을 때 Task.Result 동작 방식

by dicohy27 2023. 1. 11.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            var task3 = Method3Async();
            var task5 = Method5Async();
            //Task.WaitAll(task3, task5);
            Console.WriteLine(task3.Result + task5.Result); //WaitAll 없이도 Result때문에 기다림
            string s = Console.ReadLine();
            Console.WriteLine(s);
            Console.ReadLine();
        }
        private static Task<int> Method3Async()
        {
            return Task.Factory.StartNew(() =>
            {
                Thread.Sleep(5000);
                return 3;
            });
        }
        private static Task<int> Method5Async()
        {
            return Task.Factory.StartNew(() =>
            {
                Thread.Sleep(7000);
                return 5;
            });
        }
    }
}

'컴퓨터 > C#' 카테고리의 다른 글

this 생성 전 이벤트 방지  (1) 2023.01.11
이벤트 중복 방지 방법  (0) 2023.01.11
delegate BeginInvoke, EndInvoke  (0) 2023.01.11