Created
February 5, 2021 02:10
-
-
Save jayu108/809b32014217ad64207caeb1c7b13459 to your computer and use it in GitHub Desktop.
Task.Delay 사용과 UI blcok 관계
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace Task_Delay_test1 | |
{ | |
public partial class Form1 : Form | |
{ | |
Stopwatch stopwatch = new Stopwatch(); | |
public Form1() | |
{ | |
InitializeComponent(); | |
Thread.CurrentThread.Name = "_mainUI_"; | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
Debug.WriteLine($"--- main UI -- {Thread.CurrentThread.ManagedThreadId}"); | |
} | |
private async void button1_Click(object sender, EventArgs e) | |
{ | |
Debug.WriteLine($"---Test1 -- START --"); | |
await this.Test1(); // await 없으면, 다음 코드 바로 실행됨. | |
Debug.WriteLine($"---Test1 --- END ---"); | |
} | |
private async Task Test1() | |
{ | |
// no UI block & delay ok | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
await Task.Delay(3000); | |
//Task.Delay(3000); // delay 없이, 다음 코드 실행됨 | |
stopwatch.Stop(); | |
Debug.WriteLine($"Test1() ; {Thread.CurrentThread.ManagedThreadId} "); | |
Debug.WriteLine($" elapsed time = {stopwatch.Elapsed.Seconds}초 {stopwatch.Elapsed.Milliseconds}"); | |
} | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
Debug.WriteLine($"---Test2 -- START --"); | |
this.Test2(); // UI block 발생 | |
Debug.WriteLine($"---Test2 --- END ---"); | |
} | |
private void Test2() | |
{ | |
// UI block 발생, UI delay ok | |
var t = Task.Run(async () => | |
{ | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
await Task.Delay(5000); | |
stopwatch.Stop(); | |
Debug.WriteLine($"Thread Id : {Thread.CurrentThread.ManagedThreadId}"); | |
Debug.WriteLine($" elapsed time = {stopwatch.Elapsed.Seconds}초 {stopwatch.Elapsed.Milliseconds}"); | |
}); | |
t.Wait(); | |
Debug.WriteLine($"-----Test2-------"); | |
} | |
private void button3_Click(object sender, EventArgs e) | |
{ | |
Debug.WriteLine($"---Test3 -- START --"); | |
this.Test3(); //--- no UI block, no delay | |
Debug.WriteLine($"---Test3 --- END ---"); | |
} | |
private async void Test3() | |
{ | |
//--- no UI block #1 ---- but, await 해도 5초 delay 효과 없음. (no UI delay) | |
await Task.Factory.StartNew(async () => | |
{ | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
await Task.Delay(5000); | |
stopwatch.Stop(); | |
Debug.WriteLine($"Thread Id : {Thread.CurrentThread.ManagedThreadId}"); | |
Debug.WriteLine($" elapsed time = {stopwatch.Elapsed.Seconds}초 {stopwatch.Elapsed.Milliseconds}"); | |
}); | |
//--- no UI block #2 ---- but, t.Wait() 해도 5초 delay 효과 없음. (no UI delay) | |
//var t = Task.Factory.StartNew(async () => | |
//{ | |
// stopwatch.Reset(); | |
// stopwatch.Start(); | |
// await Task.Delay(5000); | |
// stopwatch.Stop(); | |
// Debug.WriteLine($"Thread Id : {Thread.CurrentThread.ManagedThreadId}"); | |
// Debug.WriteLine($" elapsed time = {stopwatch.Elapsed.Seconds}초 {stopwatch.Elapsed.Milliseconds}"); | |
//}); | |
//t.Wait(); | |
Debug.WriteLine($"-----Test3-------"); | |
} | |
private async void button4_Click(object sender, EventArgs e) | |
{ | |
Debug.WriteLine($"---Test4 -- START --"); | |
await this.Test4(); | |
Debug.WriteLine($"---Test4 --- END ---"); | |
} | |
private async Task Test4() | |
{ | |
// await --> no UI block | |
// Wait() --> UI block 발생 | |
for (int i = 0; i < 10; i++) | |
{ | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
//Task.Delay(200); // delay 없이, 다음 코드로 넘어감. -- no delay | |
await Task.Delay(200); // no UI block , UI delay ok | |
//Task.Delay(200).Wait(); // UI block, UI delay ok | |
stopwatch.Stop(); | |
Debug.WriteLine($"{i} ; {Thread.CurrentThread.ManagedThreadId}"); | |
Debug.WriteLine($" elapsed time = {stopwatch.Elapsed.Seconds}초 {stopwatch.Elapsed.Milliseconds}"); | |
} | |
} | |
private async void button5_Click(object sender, EventArgs e) | |
{ | |
Debug.WriteLine($"---Test5 -- START --"); | |
for (int i = 0; i < 10; i++) | |
{ | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
await this.Test5(); // await 없으면, delay 없음. | |
stopwatch.Stop(); | |
Debug.WriteLine($"Thread Id : {Thread.CurrentThread.ManagedThreadId}"); | |
Debug.WriteLine($" elapsed time = {stopwatch.Elapsed.Seconds}초 {stopwatch.Elapsed.Milliseconds}"); | |
} | |
Debug.WriteLine($"---Test5 --- END ---"); | |
} | |
// 호출시 await Test5() 로 해야지, delay 발생함. | |
private async Task Test5() | |
{ | |
await Task.Delay(500); // no UI block , UI delay ok | |
//Task.Delay(500).Wait(); // UI block, UI delay ok | |
} | |
private void button6_Click(object sender, EventArgs e) | |
{ | |
Debug.WriteLine($"---Test6 -- START --"); | |
this.Test6(); | |
Debug.WriteLine($"---Test6 --- END ---"); | |
} | |
private void Test6() | |
{ | |
// UI block. delay ok | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
Task.Delay(3000).GetAwaiter().GetResult(); // UI block, delay ok | |
stopwatch.Stop(); | |
Debug.WriteLine($"Test6() ; {Thread.CurrentThread.ManagedThreadId}"); | |
Debug.WriteLine($" elapsed time = {stopwatch.Elapsed.Seconds}초 {stopwatch.Elapsed.Milliseconds}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment