PostOperationCompleted Method
.NET Framework Class Library
AsyncOperation..::.PostOperationCompleted Method

Ends the lifetime of an asynchronous operation.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Sub PostOperationCompleted ( _
    d As SendOrPostCallback, _
    arg As Object _
)
Visual Basic (Usage)
Dim instance As AsyncOperation
Dim d As SendOrPostCallback
Dim arg As Object

instance.PostOperationCompleted(d, arg)
C#
public void PostOperationCompleted(
    SendOrPostCallback d,
    Object arg
)
Visual C++
public:
void PostOperationCompleted(
    SendOrPostCallback^ d, 
    Object^ arg
)
JScript
public function PostOperationCompleted(
    d : SendOrPostCallback, 
    arg : Object
)

Parameters

d
Type: System.Threading..::.SendOrPostCallback
A SendOrPostCallback object that wraps the delegate to be called when the operation ends.
arg
Type: System..::.Object
An argument for the delegate contained in the d parameter.
ExceptionCondition
InvalidOperationException

OperationCompleted has been called previously for this task.

ArgumentNullException

d is nullNothingnullptra null reference (Nothing in Visual Basic).

Call the PostOperationCompleted method to end the lifetime of an asynchronous operation. After this method is called for a particular task, calls to its corresponding AsyncOperation object will raise an exception.

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. 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.

The arg parameter is used to pass state information to the completion delegate d. You can use an AsyncOperation object, or an System.ComponentModel..::.AsyncCompletedEventArgs object as the parameter value. Alternatively, if you want to provide additional state storage, you can use an instance of a class you derive from the System.ComponentModel..::.AsyncCompletedEventArgs class.

Notes to Inheritors:

Inheritors must make the PostOperationCompleted 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. 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.

The following code example demonstrates using the PostOperationCompleted method to end the lifetime of an asynchronous operation. This code example is part of a larger example provided for the System.ComponentModel..::.AsyncOperationManager class.

Visual Basic
' 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
C#
// 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);
        }
    }
}

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker