평가 및 의견을 보내려면 클릭하십시오.
MSDN
MSDN Library
.NET 개발
이전 버전
.NET Framework SDK 2.0
Class Library Reference
System.ComponentModel
BackgroundWorker 클래스
BackgroundWorker 이벤트
 DoWork 이벤트

  저대역폭 보기 설정
이 페이지에서 다루는 특정 제품:.
Microsoft Visual Studio 2005/.NET Framework 2.0

다음 제품들은 다른 버전에서 다루어 집니다.
.NET Framework 클래스 라이브러리
BackgroundWorker.DoWork 이벤트

참고: 이 이벤트는 .NET Framework 버전 2.0에서 새로 추가되었습니다.

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

네임스페이스: System.ComponentModel
어셈블리: System(system.dll)

Visual Basic(선언)
Public Event DoWork As DoWorkEventHandler
Visual Basic(사용법)
Dim instance As BackgroundWorker
Dim handler As DoWorkEventHandler

AddHandler instance.DoWork, handler
C#
public event DoWorkEventHandler DoWork
C++
public:
event DoWorkEventHandler^ DoWork {
    void add (DoWorkEventHandler^ value);
    void remove (DoWorkEventHandler^ value);
}
J#
/** @event */
public void add_DoWork (DoWorkEventHandler value)

/** @event */
public void remove_DoWork (DoWorkEventHandler value)
JScript
JScript에서는 이벤트를 사용할 수 있지만 새로 선언할 수는 없습니다.

이 이벤트는 RunWorkerAsync 메서드를 호출할 때 발생합니다. 이때 수행하는 데 시간이 많이 걸릴 수 있는 작업이 시작됩니다.

DoWork 이벤트 처리기의 코드에서는 정기적으로 CancellationPending 속성 값을 확인하여 true이면 작업을 중단해야 합니다. 이때 System.ComponentModel.DoWorkEventArgsCancel 플래그를 true로 설정할 수 있으며, RunWorkerCompleted 이벤트 처리기에 있는 System.ComponentModel.RunWorkerCompletedEventArgsCancelled 플래그가 true로 설정됩니다.

Caution note주의

DoWork 이벤트 처리기의 코드에서는 취소가 요청되면 작업을 종료할 수 있으며, 폴링 루프에서 true로 설정되는 CancellationPending을 놓칠 수 있습니다. 이 경우 취소가 요청된 경우에도 RunWorkerCompleted 이벤트 처리기에 있는 System.ComponentModel.RunWorkerCompletedEventArgsCancelled 플래그가 true로 설정되지 않습니다. 이러한 상황을 경쟁 조건이라고 하는데, 경쟁 조건은 다중 스레드 프로그래밍에서 제기되는 일반적인 문제입니다. 다중 스레드 디자인 문제에 대한 자세한 내용은 관리되는 스레딩을 구현하는 최선의 방법을 참조하십시오.

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

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

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

이벤트 처리에 대한 자세한 내용은 이벤트 사용을 참조하십시오.

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

Visual Basic
' 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 'backgroundWorker1_DoWork
C#
// 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);
}
C++
// 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 );
}
J#
// 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 = (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.set_Result(new Long(ComputeFibonacci(System.Convert.ToInt32
        (e.get_Argument()), worker, e)));
    //e.Result = ComputeFibonacci((int)e.Argument, worker, e); 
} //backgroundWorker1_DoWork

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

.NET Framework

2.0에서 지원
커뮤니티 콘텐츠   커뮤니티 콘텐츠란?
새 콘텐츠 추가 RSS  주석
Processing
© 2009 Microsoft Corporation. All rights reserved. 사용약관  |  상표  |  개인정보보호
Page view tracker