... | ... | @@ -23,4 +23,40 @@ class CallerInfo |
|
|
}
|
|
|
```
|
|
|
|
|
|
:question: :eyes: :star: :hand_with_index_and_middle_finger_crossed: :muscle: :kr: :heart_eyes_cat: :ok_hand: :speak_no_evil: :bulb: :exclamation: |
|
|
\ No newline at end of file |
|
|
:question: :eyes: :star: :hand_with_index_and_middle_finger_crossed: :muscle: :kr: :heart_eyes_cat: :ok_hand: :speak_no_evil: :bulb: :exclamation:
|
|
|
|
|
|
## 비동기 호출
|
|
|
|
|
|
### async/await 예약어
|
|
|
비동기 호출에 await 예약어가 함께 쓰이면 C# 컴파일러는 이를 인지하고 그 이후의 코드를 묶어서 비동기 호출이 끝난 다음에 실행되도록 코드를 변경해서 컴파일한다. async 예약어가 있으면 컴파일러는 await 예약어를 예약어로 인식한다. async 예약어가 없으면 그냥 식별자로 인식한다.
|
|
|
|
|
|
```c#
|
|
|
class AsyncAwaitTest
|
|
|
{
|
|
|
private static async void AwaitRead()
|
|
|
{
|
|
|
using (FileStream fs = new FileStream("test.log", FileMode.Open))
|
|
|
{
|
|
|
byte[] buffer = new byte[fs.Length];
|
|
|
|
|
|
Console.WriteLine("실행 전의 스레드 id : " + Thread.CurrentThread.ManagedThreadId);
|
|
|
await fs.ReadAsync(buffer, 0, buffer.Length);
|
|
|
Console.WriteLine("실행 후의 스레드 id : " + Thread.CurrentThread.ManagedThreadId);
|
|
|
//ReadAsync 이하의 처리는 스레드 풀 스레드가 담당하기 때문에 스레드 id가 다르게 나온다.
|
|
|
|
|
|
//이하의 라인은 ReadAsync 비동기 호출이 완료된 후 호출
|
|
|
string txt = Encoding.UTF8.GetString(buffer);
|
|
|
Console.WriteLine(txt);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void Main()
|
|
|
{
|
|
|
AwaitRead();
|
|
|
Console.ReadLine();
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
|
|
|
|