AsyncOperationManager.CreateOperation(Object) 메서드

정의

특정 비동기 작업의 기간을 추적하기 위한 AsyncOperation을 반환합니다.

public:
 static System::ComponentModel::AsyncOperation ^ CreateOperation(System::Object ^ userSuppliedState);
public static System.ComponentModel.AsyncOperation CreateOperation (object userSuppliedState);
public static System.ComponentModel.AsyncOperation CreateOperation (object? userSuppliedState);
static member CreateOperation : obj -> System.ComponentModel.AsyncOperation
Public Shared Function CreateOperation (userSuppliedState As Object) As AsyncOperation

매개 변수

userSuppliedState
Object

작업 ID와 같은 클라이언트 상태 정보와 특정 비동기 작업을 연결하는 데 사용되는 개체입니다.

반환

비동기 메서드 호출의 기간을 추적하는 데 사용할 수 있는 AsyncOperation입니다.

예제

다음 코드 예제에서는 메서드를 CreateOperation 사용하여 비동기 작업의 기간을 추적하기 위한 을 만드는 System.ComponentModel.AsyncOperation 방법을 보여 줍니다. 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 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

설명

합니다 CreateOperation 메서드가 반환 되는 System.ComponentModel.AsyncOperation 특정 비동기 작업의 기간을 추적 하 고 애플리케이션 모델은 작업이 완료 될 때 경고를 사용할 수 있는 합니다. 작업을 종료하지 않고 진행률 업데이트 및 증분 결과를 게시하는 데 사용할 수도 있습니다. System.ComponentModel.AsyncOperation 이러한 호출을 적절 한 스레드 또는 애플리케이션 모델에 대 한 상황에 맞는 올바르게 마샬링해야 합니다.

이벤트 기반 비동기 패턴을 지원하는 클래스를 구현하는 경우 MethodNameAsync 메서드가 호출 될 때마다 클래스가 호출 CreateOperation 되어야 합니다. 메서드를 호출 하는 클라이언트 애플리케이션에서 사용할 수는 userSuppliedState 매개 변수를 각 호출을 비동기 작업을 실행 하는 동안 발생 하는 이벤트를 구분할를 고유 하 게 식별 합니다.

주의

클라이언트 코드는 매개 변수에 고유한 userSuppliedState 값을 제공해야 합니다. 고유하지 않은 작업 ID로 인해 구현에서 진행률 및 기타 이벤트를 잘못 보고할 수 있습니다. 코드는 고유하지 않은 작업 ID에 대해 검사 검색되면 을 System.ArgumentException throw해야 합니다.

코드는 에서 반환된 CreateOperation 모든 System.ComponentModel.AsyncOperation 를 추적하고 해당 기본 비동기 작업에서 개체를 사용하여 업데이트를 게시하고 작업을 종료해야 합니다. 이 추적은 대리자 간에 매개 변수로 를 System.ComponentModel.AsyncOperation 전달하는 것만큼 간단할 수 있습니다. 보다 정교한 디자인에서 클래스는 개체 컬렉션을 System.ComponentModel.AsyncOperation 유지 관리하고, 작업이 시작될 때 개체를 추가하고, 작업이 완료되거나 취소될 때 개체를 제거할 수 있습니다. 이 방법을 사용하면 고유한 userSuppliedState 매개 변수 값에 대해 검사 수 있으며 여러 동시 호출을 지원하는 클래스로 작업할 때 사용해야 하는 메서드입니다.

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

적용 대상

추가 정보