Task 공부중인데요
아래 코드에서 왜 wait이 안되는지 이해가 잘 안되는데 이유가 뭔가요?
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Application thread ID: {0}", Thread.CurrentThread.ManagedThreadId);
var t = Task.Run(() => {
PrintCount(5);
});
Console.WriteLine("넘어가나");
t.Wait(); // <---- 왜 안기다리지?
Console.WriteLine("기다리나?");
}
static async void PrintCount(int count)
{
for (int i = 0; i < count; i++)
{
await Task.Delay(100 * new Random().Next(3, 10)); //0.3 ~ 1초 딜레이
Console.WriteLine($"count {i} id:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
}
Console.WriteLine($"count end id:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
}
출력 결과
넘어가나
기다리나?
count 0 id:4
count 1 id:3
count 2 id:4
count 3 id:3
count 4 id:3
count end id:3