AsyncOperation.PostOperationCompleted(SendOrPostCallback, Object) 方法

定义

结束异步操作的生存期。Ends the lifetime of an asynchronous operation.

public:
 void PostOperationCompleted(System::Threading::SendOrPostCallback ^ d, System::Object ^ arg);
public void PostOperationCompleted (System.Threading.SendOrPostCallback d, object arg);
public void PostOperationCompleted (System.Threading.SendOrPostCallback d, object? arg);
member this.PostOperationCompleted : System.Threading.SendOrPostCallback * obj -> unit
Public Sub PostOperationCompleted (d As SendOrPostCallback, arg As Object)

参数

d
SendOrPostCallback

一个用于包装操作结束时要调用的委托的 SendOrPostCallback 对象。A SendOrPostCallback object that wraps the delegate to be called when the operation ends.

arg
Object

d 参数中包含的委托的一个自变量。An argument for the delegate contained in the d parameter.

例外

此前便已为此任务调用了 OperationCompleted()OperationCompleted() has been called previously for this task.

dnulld is null.

示例

下面的代码示例演示如何使用 PostOperationCompleted 方法结束异步操作的生存期。The following code example demonstrates using the PostOperationCompleted method to end the lifetime of an asynchronous operation. 此代码示例是为类提供的更大示例的一部分 System.ComponentModel.AsyncOperationManagerThis code example is part of a larger example provided for the System.ComponentModel.AsyncOperationManager class.

// This method cancels a pending asynchronous operation.
public void CancelAsync(object taskId)
{
    AsyncOperation asyncOp = userStateToLifetime[taskId] as AsyncOperation;
    if (asyncOp != null)
    {   
        lock (userStateToLifetime.SyncRoot)
        {
            userStateToLifetime.Remove(taskId);
        }
    }
}
' This method cancels a pending asynchronous operation.
Public Sub CancelAsync(ByVal taskId As Object)

    Dim obj As Object = userStateToLifetime(taskId)
    If (obj IsNot Nothing) Then

        SyncLock userStateToLifetime.SyncRoot

            userStateToLifetime.Remove(taskId)

        End SyncLock

    End If

End Sub

注解

调用 PostOperationCompleted 方法以结束异步操作的生存期。Call the PostOperationCompleted method to end the lifetime of an asynchronous operation. 对特定任务调用此方法后,对其相应对象的调用 AsyncOperation 将引发异常。After this method is called for a particular task, calls to its corresponding AsyncOperation object will raise an exception.

d当任务的生存期由于任务完成、取消或失败而结束时,参数会包装你希望类调用的委托。The d parameter wraps the delegate you want your class to call when the task's lifetime ends due to completion, cancellation, or failure of the task. AsyncOperation对象将确保在适用于应用程序模型的线程或上下文中调用委托。The AsyncOperation object will ensure that your delegate is invoked on the thread or context appropriate for the application model. 委托可以选择引发事件,以通知客户端异步任务的生存期已结束。Your delegate can optionally raise an event that notifies clients that the asynchronous task's lifetime has ended.

arg参数用于将状态信息传递给完成委托 dThe arg parameter is used to pass state information to the completion delegate d. 可以使用 AsyncOperation 对象或 System.ComponentModel.AsyncCompletedEventArgs 对象作为参数值。You can use an AsyncOperation object, or an System.ComponentModel.AsyncCompletedEventArgs object as the parameter value. 或者,如果要提供附加状态存储,可以使用从类派生的类的实例 System.ComponentModel.AsyncCompletedEventArgsAlternatively, if you want to provide additional state storage, you can use an instance of a class you derive from the System.ComponentModel.AsyncCompletedEventArgs class.

继承者说明

继承者必须使 PostOperationCompleted(SendOrPostCallback, Object) 调用异步,以便类库提供程序在假定异步但特定应用程序模型发生同步时,无需考虑潜在的堆栈溢出。Inheritors must make the PostOperationCompleted(SendOrPostCallback, Object) invocation asynchronous, so that class library providers do not need to concern themselves with potential stack overflows if they assume asynchrony but a particular application model happens to be synchronous. 方法应解释为 "结束生存期" 调用,这意味着实现需要执行适用于应用程序模型的操作。The method should be interpreted as an "ending the lifetime" call, meaning the implementation needs to do what is appropriate for the application model. 例如,ASP.NET 将减少未完成的异步操作的计数。For instance, ASP.NET will decrement its count of outstanding asynchronous operations. 这还应将操作置于状态,以便对其进行的任何后续调用都将失败,因为它现在已完成。This also should put the operation into a state such that any subsequent calls into it will fail, since it has now completed.

有关实现异步类的详细信息,请参阅 实现基于事件的异步模式For more information about implementing asynchronous classes, see Implementing the Event-based Asynchronous Pattern.

适用于

另请参阅