... | ... | @@ -1601,3 +1601,49 @@ class EventWaitHandleEx2 //ThreadPool에서 EventWaitHandler로 Join()메서드 |
|
|
```
|
|
|
|
|
|
### 비동기 호출
|
|
|
디스크를 읽는 동안 스레드의 제어가 반환되지 않아서 스레드는 대기 상태로 머물러 있는다. 이를 동기 호출이라고 하는데, 비동기 호출은 디스크를 읽더라도 스레드는 계속해서 실행되고, 읽기 작업이 완료되면 ThreadPool로 부터 유휴 스레드를 얻어와 그 스레드에서 읽기 이후의 나머지 실행을 맡긴다.
|
|
|
|
|
|
비동기 호출 실행 예제
|
|
|
|
|
|
```c#
|
|
|
class FileState
|
|
|
{
|
|
|
public byte[] buffer;
|
|
|
public FileStream file;
|
|
|
}
|
|
|
|
|
|
class AsyncCallEx1
|
|
|
{
|
|
|
public static void Main()
|
|
|
{
|
|
|
FileStream fs = new FileStream("test.log", FileMode.Open);
|
|
|
|
|
|
FileState state = new FileState();
|
|
|
state.buffer = new Byte[fs.Length];
|
|
|
state.file = fs;
|
|
|
|
|
|
fs.BeginRead(state.buffer, 0, state.buffer.Length, ReadCompleted, state);
|
|
|
//읽은 데이터를 저장할 버퍼, 읽기를 시작할 바이트 위치, 읽을 바이트의 길이, 비동기 요청이 끝나면 실행될 메서드, 비동기 요청을 구분할 수 있는 객체
|
|
|
|
|
|
Console.WriteLine("비동기 호출 실행중!");
|
|
|
|
|
|
Console.ReadLine(); //비동기이기 때문에 읽기 작업과는 별개로 실행된다.
|
|
|
|
|
|
fs.Close();
|
|
|
}
|
|
|
|
|
|
//읽기 작업이 완료되면 호출되는 메서드이다. 스레드 풀의 스레드에서 실행된다.
|
|
|
static void ReadCompleted(IAsyncResult ar)
|
|
|
{
|
|
|
Console.WriteLine("비동기 호출 실행 완료!");
|
|
|
FileState state = ar.AsyncState as FileState; //state로 전달된 객체를 형변환을 통해 받을 수 있다.
|
|
|
|
|
|
state.file.EndRead(ar); //비동기 읽기 작업이 끝나기를 기다린다. BeginRead를 썼으면 꼭 써줘야한다.
|
|
|
//그렇지 않으면 교착 상태 같이 원치 않는 동작이 발생할 수 있다.
|
|
|
|
|
|
string txt = Encoding.UTF8.GetString(state.buffer);
|
|
|
Console.WriteLine(txt); //"test.log"에 있던 문자열들이 출력된다.
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|