AsyncOperation 클래스

정의

비동기 작업의 수명을 추적합니다.

public ref class AsyncOperation sealed
public sealed class AsyncOperation
type AsyncOperation = class
Public NotInheritable Class AsyncOperation
상속
AsyncOperation

예제

다음 코드 예제에서는 개체를 AsyncOperation 사용하여 비동기 작업의 수명을 추적하는 방법을 보여 줍니다. 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 System.ComponentModel.AsyncOperationManager 클래스입니다.

전체 코드 목록은 방법: 이벤트 기반 비동기 패턴을 지원하는 구성 요소 구현을 참조하세요. 클라이언트 양식의 전체 코드 목록은 방법: 이벤트 기반 비동기 패턴의 클라이언트 구현을 참조하세요.

// This method starts an asynchronous calculation. 
// First, it checks the supplied task ID for uniqueness.
// If taskId is unique, it creates a new WorkerEventHandler 
// and calls its BeginInvoke method to start the calculation.
public virtual void CalculatePrimeAsync(
    int numberToTest,
    object taskId)
{
    // Create an AsyncOperation for taskId.
    AsyncOperation asyncOp =
        AsyncOperationManager.CreateOperation(taskId);

    // Multiple threads will access the task dictionary,
    // so it must be locked to serialize access.
    lock (userStateToLifetime.SyncRoot)
    {
        if (userStateToLifetime.Contains(taskId))
        {
            throw new ArgumentException(
                "Task ID parameter must be unique", 
                "taskId");
        }

        userStateToLifetime[taskId] = asyncOp;
    }

    // Start the asynchronous operation.
    WorkerEventHandler workerDelegate = new WorkerEventHandler(CalculateWorker);
    workerDelegate.BeginInvoke(
        numberToTest,
        asyncOp,
        null,
        null);
}
' This method starts an asynchronous calculation. 
' First, it checks the supplied task ID for uniqueness.
' If taskId is unique, it creates a new WorkerEventHandler 
' and calls its BeginInvoke method to start the calculation.
Public Overridable Sub CalculatePrimeAsync( _
    ByVal numberToTest As Integer, _
    ByVal taskId As Object)

    ' Create an AsyncOperation for taskId.
    Dim asyncOp As AsyncOperation = _
        AsyncOperationManager.CreateOperation(taskId)

    ' Multiple threads will access the task dictionary,
    ' so it must be locked to serialize access.
    SyncLock userStateToLifetime.SyncRoot
        If userStateToLifetime.Contains(taskId) Then
            Throw New ArgumentException( _
                "Task ID parameter must be unique", _
                "taskId")
        End If

        userStateToLifetime(taskId) = asyncOp
    End SyncLock

    ' Start the asynchronous operation.
    Dim workerDelegate As New WorkerEventHandler( _
        AddressOf CalculateWorker)

    workerDelegate.BeginInvoke( _
        numberToTest, _
        asyncOp, _
        Nothing, _
        Nothing)

End Sub

설명

이벤트 기반 비동기 패턴 개요에 따라 클래스를 구현하는 경우 클래스 인스턴스에서 호출된 각 비동기 작업의 수명을 추적해야 할 수 있습니다. 클래스는 AsyncOperation 비동기 작업의 진행률을 추적하고 보고하는 방법을 제공합니다.

다음 목록에서는 개체를 사용하는 AsyncOperation 방법을 식별합니다.

  • 진행률 및 중간 결과를 클라이언트에 보고하려면 비동기 작업자 코드에서 호출 Post 합니다.

  • 비동기 작업이 완료되었음을 나타내거나 보류 중인 비동기 작업을 취소하려면 다음을 호출 PostOperationCompleted합니다.

클래스는 각 작업이 시작될 때 호출 AsyncOperationManager.CreateOperation 하여 각 비동기 작업에 대한 개체를 가져와 AsyncOperation 야 합니다. 클라이언트가 별도의 비동기 작업을 AsyncOperationManager.CreateOperation 구분할 수 있도록 하려면 속성이 되는 고유한 클라이언트 제공 토큰에 대한 매개 변수를 UserSuppliedState 사용합니다. 그런 다음 클라이언트 코드에서 진행률 또는 완료 이벤트를 발생시키는 특정 비동기 작업을 식별하는 데 사용할 수 있습니다.

상속자 참고

구현 자가 있어야 합니다 PostOperationCompleted(SendOrPostCallback, Object)Post(SendOrPostCallback, Object) 호출이 비동기가 특정 애플리케이션에 비동기 동작 가정 하는 경우 잠재적인 스택 오버플로 클래스 라이브러리 공급자를 사용 하 여 고려할 필요가 없습니다 동기로 발생 하는 모델입니다.

비동기 클래스 구현에 대한 자세한 내용은 이벤트 기반 비동기 패턴 구현을 참조하세요.

속성

SynchronizationContext

생성자에 전달된 SynchronizationContext 개체를 가져옵니다.

UserSuppliedState

비동기 작업을 고유하게 식별하는 데 사용되는 개체를 가져오거나 설정합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
Finalize()

비동기 작업을 완료합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
OperationCompleted()

비동기 작업의 수명을 끝냅니다.

Post(SendOrPostCallback, Object)

애플리케이션 모델에 적절한 스레드나 컨텍스트에서 대리자를 호출합니다.

PostOperationCompleted(SendOrPostCallback, Object)

비동기 작업의 수명을 끝냅니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보