BackgroundWorker.DoWork 이벤트

정의

RunWorkerAsync()가 호출될 때 발생합니다.

public:
 event System::ComponentModel::DoWorkEventHandler ^ DoWork;
public event System.ComponentModel.DoWorkEventHandler DoWork;
public event System.ComponentModel.DoWorkEventHandler? DoWork;
member this.DoWork : System.ComponentModel.DoWorkEventHandler 
Public Custom Event DoWork As DoWorkEventHandler 

이벤트 유형

예제

다음 코드 예제에서는 이벤트를 사용하여 DoWork 비동기 작업을 시작하는 방법을 보여 줍니다. 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 BackgroundWorker 클래스입니다.

// This event handler is where the actual,
// potentially time-consuming work is done.
void backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
   // Get the BackgroundWorker that raised this event.
   BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);

   // Assign the result of the computation
   // to the Result property of the DoWorkEventArgs
   // object. This is will be available to the 
   // RunWorkerCompleted eventhandler.
   e->Result = ComputeFibonacci( safe_cast<Int32>(e->Argument), worker, e );
}
// This event handler is where the actual,
// potentially time-consuming work is done.
private void backgroundWorker1_DoWork(object sender, 
    DoWorkEventArgs e)
{   
    // Get the BackgroundWorker that raised this event.
    BackgroundWorker worker = sender as BackgroundWorker;

    // Assign the result of the computation
    // to the Result property of the DoWorkEventArgs
    // object. This is will be available to the 
    // RunWorkerCompleted eventhandler.
    e.Result = ComputeFibonacci((int)e.Argument, worker, e);
}
' This event handler is where the actual work is done.
Private Sub backgroundWorker1_DoWork( _
ByVal sender As Object, _
ByVal e As DoWorkEventArgs) _
Handles backgroundWorker1.DoWork

    ' Get the BackgroundWorker object that raised this event.
    Dim worker As BackgroundWorker = _
        CType(sender, BackgroundWorker)

    ' Assign the result of the computation
    ' to the Result property of the DoWorkEventArgs
    ' object. This is will be available to the 
    ' RunWorkerCompleted eventhandler.
    e.Result = ComputeFibonacci(e.Argument, worker, e)
End Sub

설명

이 이벤트는 메서드를 호출할 때 발생합니다 RunWorkerAsync . 여기서는 잠재적으로 시간이 많이 걸리는 작업을 수행하는 작업을 시작합니다.

이벤트 처리기의 코드는 DoWork 속성 값을 주기적으로 검사 CancellationPending 이면 true작업을 중단해야 합니다. 이 경우 의 System.ComponentModel.DoWorkEventArgstrue플래그를 Cancel 로 설정할 수 있으며 Cancelled 이벤트 처리기의 플래그 System.ComponentModel.RunWorkerCompletedEventArgsRunWorkerCompleted 는 로 true설정됩니다.

주의

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

작업이 결과를 생성하는 경우 결과를 속성에 할당할 DoWorkEventArgs.Result 수 있습니다. 속성의 RunWorkerCompleted 이벤트 처리기에서 RunWorkerCompletedEventArgs.Result 사용할 수 있습니다.

작업에서 코드가 처리하지 않는 예외가 발생하는 경우 는 BackgroundWorker 예외를 catch하고 이벤트 처리기에 전달 RunWorkerCompleted 합니다. 여기서 는 의 System.ComponentModel.RunWorkerCompletedEventArgs속성으로 Error 노출됩니다. Visual Studio 디버거에서 실행하는 경우 처리되지 않은 예외가 발생한 이벤트 처리기의 지점에서 DoWork 디버거가 중단됩니다. 둘 BackgroundWorker이상의 가 있는 경우 이벤트 처리기를 의 특정 instance BackgroundWorker결합 DoWork 하므로 직접 참조해서는 안 됩니다. 대신 이벤트 처리기에서 매개 변수를 캐스팅하여 액세스해야 합니다 BackgroundWorkerDoWork.sender

이벤트 처리기에서 사용자 인터페이스 개체를 조작하지 않도록 주의해야 합니다 DoWork . 그 대신 BackgroundWorker 이벤트를 통해 사용자 인터페이스와 통신해야 합니다.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보