Lập trình bất đồng bộ (async) hiệu quả cho hoạt động bị block hoặc delay. Sync process bị block -> toàn app chờ, mất thời gian, app stop responding. Async cho phép app tiếp tục task khác.
GUI app check queue, lấy unprocessed task xử lý trước. Code sync -> unprocessed task hoàn thành trước. Nếu xử lý lâu -> app "stop responding".
Ví dụ sync (vấn đề)
private void OnRequestDownload(object sender, RoutedEventArgs e) {
var req = HttpWebRequest.Create(_requestedUri);
var res = req.GetResponse(); // blocking
}
Giải pháp: async/await
private async void OnRequestDownload(object sender, RoutedEventArgs e) {
var req = HttpWebRequest.Create(_requestedUri);
var res = await req.GetResponseAsync(); // non-blocking
}