async void는 되도록이면 사용하지 말자.
Task를 이용해 비동기 코드를 작성할 때, async void는 되도록이면 사용하지 않는 것이 좋다. async void가 가진 문제점들은 다음과 같다. 1. Task를 반환하지 않으므로 호출자에서 await을 통해 완료 되었는지 알 수 없다. 누군가가 아래와 같이 스트림에 바이트 배열을 기록하는 클래스를 만들었다고 해보자. public class DataWriter { public async void WriteDataAsync(byte[] data); public int Length { get; private set; } } 만약 WriteDataAsync 비동기 함수의 작업이 언제 끝나는지 알고싶다면 어떻게 해야할까? 방법이 없다. Polling을 통해 데이터가 모두 기록되었는지 확인하는 등 비효율..
2024.03.30