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",
                nameof(taskId));
        }

        userStateToLifetime[taskId] = asyncOp;
    }

    // Start the asynchronous operation.
    WorkerEventHandler workerDelegate = new(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 모델에 대한 적절한 스레드 또는 컨텍스트로 이러한 호출을 올바르게 마샬링합니다.

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

Caution

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

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

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

적용 대상

추가 정보