다음을 통해 공유


BackgroundWorker.CancelAsync 메서드

정의

보류 중인 백그라운드 작업의 취소를 요청합니다.

public:
 void CancelAsync();
public void CancelAsync();
member this.CancelAsync : unit -> unit
Public Sub CancelAsync ()

예외

예제

다음 코드 예제에서는 메서드를 사용하여 CancelAsync 비동기("백그라운드") 작업을 취소하는 방법을 보여 줍니다. 이 코드 예제는 클래스에 제공된 더 큰 예제의 BackgroundWorker 일부입니다.

void cancelAsyncButton_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{  
   // Cancel the asynchronous operation.
   this->backgroundWorker1->CancelAsync();
   
   // Disable the Cancel button.
   cancelAsyncButton->Enabled = false;
}
void cancelAsyncButton_Click(object sender,
    EventArgs e)
{
    // Cancel the asynchronous operation.
    backgroundWorker1.CancelAsync();

    // Disable the Cancel button.
    cancelAsyncButton.Enabled = false;
}
Private Sub cancelAsyncButton_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) _
Handles cancelAsyncButton.Click

    ' Cancel the asynchronous operation.
    backgroundWorker1.CancelAsync()

    ' Disable the Cancel button.
    cancelAsyncButton.Enabled = False

End Sub

설명

CancelAsync는 보류 중인 백그라운드 작업을 종료하는 요청을 제출하고 속성을 true.로 설정합니다CancellationPending.

호출 CancelAsync할 때 작업자 메서드는 실행을 중지하고 종료할 수 있습니다. 작업자 코드는 속성을 주기적으로 확인하여 속성이 CancellationPending 설정되었는지 확인해야 합니다 true.

주의

이벤트 처리기의 코드 DoWork 는 취소 요청이 수행될 때 해당 작업을 완료할 수 있으며 폴링 루프가 설정true되지 않을 수 있습니다CancellationPending. 이 경우 취소 요청이 Cancelled 발생하더라도 이벤트 처리기의 플래그 System.ComponentModel.RunWorkerCompletedEventArgsRunWorkerCompleted 는 설정 true되지 않습니다. 이 상황을 경합 상태 라고 하며 다중 스레드 프로그래밍에서 일반적인 관심사입니다. 다중 스레딩 디자인 문제에 대한 자세한 내용은 관리되는 스레딩 모범 사례를 참조하세요.

적용 대상

추가 정보