Export (0) Print
Expand All

AsyncOperation.Post Method

Invokes a delegate on the thread or context appropriate for the application model.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

Public Sub Post ( _
	d As SendOrPostCallback, _
	arg As Object _


Type: System.Threading.SendOrPostCallback

A SendOrPostCallback object that wraps the delegate to be called when the operation ends.

Type: System.Object

An argument for the delegate contained in the d parameter.


The PostOperationCompleted method has been called previously for this task.


d is Nothing.

The Post method invokes the delegate specified by the arg parameter without ending the lifetime of the asynchronous operation.

You can call the Post method as often as you like while the lifetime of the asynchronous operation has not been ended by a call to PostOperationCompleted. You can use the method to report progress or interim results back to clients.

The d parameter wraps the delegate you want called when you want to post an update about the status of the asynchronous task. The AsyncOperation object will ensure that your delegate is invoked on the thread or context appropriate for the application model. Your method can optionally raise an event that notifies clients of a status change, progress update, or newly available incremental results.

The arg parameter should be used to pass state to the delegate wrapped by the d parameter. It might be a reference to an AsyncOperation, or it might be a System.ComponentModel.ProgressChangedEventArgs object. It may be desirable to derive your own class from System.ComponentModel.ProgressChangedEventArgs to provide additional state storage.

Notes to Inheritors

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


Console applications do not synchronize the execution of Post calls. This can cause ProgressChanged events to be raised out of order. If you wish to have serialized execution of Post calls, implement and install a System.Threading.SynchronizationContext class.

For more information about implementing asynchronous classes, see Implementing the Event-based Asynchronous Pattern.

The following code example demonstrates using the Post method for reporting progress and incremental results of an asynchronous operation. This code example is part of a larger example provided for the System.ComponentModel.AsyncOperationManager class.

' This method computes the list of prime numbers used by the 
' IsPrime method. 
Private Function BuildPrimeNumberList( _
    ByVal numberToTest As Integer, _
    ByVal asyncOp As AsyncOperation) As ArrayList

    Dim e As ProgressChangedEventArgs = Nothing 
    Dim primes As New ArrayList
    Dim firstDivisor As Integer 
    Dim n As Integer = 5

    ' Add the first prime numbers.

    ' Do the work. 
    While n < numberToTest And _
        Not Me.TaskCanceled(asyncOp.UserSuppliedState)

        If IsPrime(primes, n, firstDivisor) Then 
            ' Report to the client that you found a prime.
            e = New CalculatePrimeProgressChangedEventArgs( _
                n, _
                CSng(n) / CSng(numberToTest) * 100, _

            asyncOp.Post(Me.onProgressReportDelegate, e)


            ' Yield the rest of this time slice.
        End If 

        ' Skip even numbers.
        n += 2

    End While 

    Return primes

End Function

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft