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.
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중단해야 합니다. 이 경우 플래그 CancelSystem.ComponentModel.DoWorkEventArgstrue 설정할 수 있으며 Cancelled 이벤트 처리기의 플래그 System.ComponentModel.RunWorkerCompletedEventArgsRunWorkerCompleted 는 .로 설정true됩니다.

주의

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

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

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

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

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

적용 대상

추가 정보