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;
}
private void cancelAsyncButton_Click(System.Object sender, 
    System.EventArgs e)
{   
    // Cancel the asynchronous operation.
    this.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.
    Me.backgroundWorker1.CancelAsync()

    ' Disable the Cancel button.
    cancelAsyncButton.Enabled = False
    
End Sub

설명

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

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

주의

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

적용 대상

추가 정보