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 void Post (
	SendOrPostCallback d,
	Object arg
)
public void Post (
	SendOrPostCallback d, 
	Object arg
)
public function Post (
	d : SendOrPostCallback, 
	arg : Object
)
Not applicable.

Parameters

d

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

arg

An argument for the delegate contained in the d parameter.

Exception typeCondition

InvalidOperationException

The PostOperationCompleted method has been called previously for this task.

ArgumentNullException

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

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.

NoteNote:

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 Asynchronous Pattern for Components.

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 ArrayList BuildPrimeNumberList(
    int numberToTest,
    AsyncOperation asyncOp)
{
    ProgressChangedEventArgs e = null;
    ArrayList primes = new ArrayList();
    int firstDivisor;
    int n = 5;

    // Add the first prime numbers.
    primes.Add(2);
    primes.Add(3);

    // Do the work.
    while (n < numberToTest && 
           !TaskCanceled( asyncOp.UserSuppliedState ) )
    {
        if (IsPrime(primes, n, out firstDivisor))
        {
            // Report to the client that a prime was found.
            e = new CalculatePrimeProgressChangedEventArgs(
                n,
                (int)((float)n / (float)numberToTest * 100),
                asyncOp.UserSuppliedState);

            asyncOp.Post(this.onProgressReportDelegate, e);

            primes.Add(n);

            // Yield the rest of this time slice.
            Thread.Sleep(0);
        }

        // Skip even numbers.
        n += 2;
    }

    return primes;
}

// This method computes the list of prime numbers used by the
// IsPrime method.
private ArrayList BuildPrimeNumberList(int numberToTest, 
    AsyncOperation asyncOp)
{
    ProgressChangedEventArgs e = null;
    ArrayList primes = new ArrayList();
    int firstDivisor = 1;
    int n = 5;
    
    // Add the first prime numbers.
    primes.Add((Int32)2);
    primes.Add((Int32)3);

    // Do the work.
    while (n < numberToTest &&
           !TaskCanceled( asyncOp.get_UserSuppliedState()) )
    {
        if (IsPrime(primes, n, /** @out */firstDivisor)) 
        {
            // Report to the client that you found a prime.
            e = new CalculatePrimeProgressChangedEventArgs(n, 
                (int)((float)n / ( float)(numberToTest) * 100), 
                asyncOp.get_UserSuppliedState());

            asyncOp.Post(this.onProgressReportDelegate, e);

            primes.Add((Int32)n);

            // Yield the rest of this time slice.
            System.Threading.Thread.Sleep(0);
        }

        // Skip even numbers.
        n += 2;

    }
    return primes;
} 

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft