일정 기간 이후 비동기 작업 취소
일정 기간 이후 비동기 작업 취소(C#)
일정 기간 이후 비동기 작업 취소(C#)Cancel async tasks after a period of time (C#) 이 문서의 내용 --> 작업이 완료될 때까지 대기하지 않으려는 경우 일정 기간 후에 CancellationTokenSource.CancelAfter 메서드를 사용하여 비동기 작업을 취소할 수 있습니다.You can cancel an asynchronous operation after a period of time by using th
docs.microsoft.com
private async void startButton_Click(object sender, RoutedEventArgs e)
{
// Instantiate the CancellationTokenSource.
cts = new CancellationTokenSource();
resultsTextBox.Clear();
try
{
// ***Set up the CancellationTokenSource to cancel after 2.5 seconds. (You
// can adjust the time.)
cts.CancelAfter(2500);
await AccessTheWebAsync(cts.Token);
resultsTextBox.Text += "\r\nDownloads succeeded.\r\n";
}
catch (OperationCanceledException)
{
resultsTextBox.Text += "\r\nDownloads canceled.\r\n";
}
catch (Exception)
{
resultsTextBox.Text += "\r\nDownloads failed.\r\n";
}
cts = null;
}
'C#' 카테고리의 다른 글
async 및 await를 사용한 비동기 프로그래밍 - 5 ( Task.WhenAny ) (0) | 2019.09.11 |
---|---|
async 및 await를 사용한 비동기 프로그래밍 - 4 ( Task.WhenAny ) (0) | 2019.09.11 |
async 및 await를 사용한 비동기 프로그래밍 - 2 (0) | 2019.09.11 |
async 및 await를 사용한 비동기 프로그래밍 - 1 (0) | 2019.09.10 |
Async Task (0) | 2019.09.10 |