IAsyncOperation<TResult> IAsyncOperation<TResult> IAsyncOperation<TResult> IAsyncOperation<TResult> Interface

Represents an asynchronous operation, which returns a result upon completion. This is the return type for many Windows Runtime asynchronous methods that have results but don't report progress.

Syntax

Declaration

public interface IAsyncOperation<TResult>public interface IAsyncOperation<TResult>Public Interface IAsyncOperation<TResult>

Inheritance Hierarchy

Remarks

IAsyncOperation<TResult> is the return type for many Windows Runtime asynchronous methods that have a result upon completion, but don't report progress. This constitutes over 650 different Windows Runtime APIs. APIs that do report progress (and have a result) use another interface, IAsyncOperationWithProgress<TResult, TProgress>.

When you use methods that return IAsyncOperation<TResult> (with a TResult specific constraint) in your app code, you usually don't access the IAsyncOperation<TResult> return value directly. That's because you almost always use the language-specific awaitable syntax. In this case, the apparent return value of the method is the type provided as the TResult parameter. For more info, see Asynchronous programming, or one of the language-specific guides to Windows Runtime asynchronous programming (Call asynchronous APIs in C# or Visual Basic, C++, JavaScript).

It's not common to use IAsyncOperation<TResult> directly even if you don't use a language-specific awaitable syntax. Each of the languages has extension points that are generally easier to use than the Windows Runtime interface. JavaScript has WinJS.Promise, and the then/done syntax. .NET has the AsTask extension method, and once the IAsyncOperation<TResult> is converted to a Task<TResult>, it's easier to get the result, cancel, get notification on completion, and so on. For C++/CX, you can wrap the calls using the Concurrency runtime (and use create_task). In other words, IAsyncOperation<TResult> can be considered runtime-level infrastructure, which each of the languages use as a framework to support awaitable syntax or asynchronous programming models in their own way.

Instead of using IAsyncOperation<TResult>, some Windows Runtime asynchronous methods use custom operation types. For example, DataReaderLoadOperation is a Windows Runtime type that implements IAsyncOperation<TResult> using uint as the result type. The DataReaderLoadOperation type is then used as the custom operation/result type for the LoadAsync(System.UInt32) method.

Interface inheritance

IAsyncOperation<TResult> inherits IAsyncInfo. Types that implement IAsyncOperation<TResult> also implement the interface members of IAsyncInfo:

Notes to implementers

As with calling the existing methods, there are language-specific ways to define asynchronous methods that don't use IAsyncOperation<TResult> directly. If writing code using .NET, your method can return a Task<TResult>. For C++/CX, you can use the Concurrency runtime. However, if you're defining a component, you can use Task/task internally but you must return one of the Windows Runtime interfaces for your public methods. The language-specific asynchronous support types (and many other language-specific types you might conventionally use in code) can't be used for the public surface area of a Windows Runtime component.

Properties summary

Gets or sets the method that handles the operation completed notification.

Methods summary

Returns the results of the operation.

Properties

  • Completed
    Completed
    Completed
    Completed

    Gets or sets the method that handles the operation completed notification.

    public AsyncOperationCompletedHandler<TResult> Completed { get; set; }public AsyncOperationCompletedHandler<TResult> Completed { get; set; }Public ReadWrite Property Completed As AsyncOperationCompletedHandler<TResult>

    Property Value

    • The method that handles the notification.

    Remarks

    The Windows Runtime enforces that this property can only be set once on an operation.

    Generally, a completed IAsyncOperation<TResult> method called using awaitable syntax does nothing further than to return its result (an object of the TResult type) when it completes.

    If you're implementing IAsyncOperation<TResult>, then the set implementation of Completed should store the handler, and the surrounding logic should invoke it when Close() is called. The implementation should set the asyncStatus parameter of invoked callbacks appropriately if there is a Cancel() call, Status is not Completed, errors occurred, and so on.

Methods

  • GetResults()
    GetResults()
    GetResults()
    GetResults()

    Returns the results of the operation.

    public TResult GetResults()public TResult GetResults()Public Function GetResults() As TResult

    Returns

    • TResult
      TResult
      TResult
      TResult

      The results of the operation.

    Remarks

    Notes to implementers

    Implementers should return the specific value that the operation type should return to callers upon completion. When the method that uses the custom operation type returns, while using an awaitable syntax (the usual usage) this provides the return value of the operation to the method caller.

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.FoundationContract (introduced v1)

Attributes

Windows.Foundation.Metadata.GuidAttribute
Windows.Foundation.Metadata.ContractVersionAttribute

Details

Assembly

Windows.Foundation.dll