User Tools

Site Tools


notes:uwp:iasync_interfaces

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:uwp:iasync_interfaces [2016/12/29]
admin
notes:uwp:iasync_interfaces [2019/01/06] (current)
leszek
Line 81: Line 81:
 The above extension methods are defined in the class //​WindowsRuntimeSystemExtensions//​ located in the //System// namespace. They return a //​TaskAwaiter//​ object which is awaited in the code. Once the asynchronous operation completes, the //​TaskAwaiter//​ object ensures that the code continues executing via the //​SynchronizationContext//​ that is associated with the original thread. Then the thread queries the Task's //Result// property, which returns the result. The above extension methods are defined in the class //​WindowsRuntimeSystemExtensions//​ located in the //System// namespace. They return a //​TaskAwaiter//​ object which is awaited in the code. Once the asynchronous operation completes, the //​TaskAwaiter//​ object ensures that the code continues executing via the //​SynchronizationContext//​ that is associated with the original thread. Then the thread queries the Task's //Result// property, which returns the result.
  
 +
 +The //AsTask// method converts the //​Windows.Foundation.IAsyncAction//​ and //​Windows.Foundation.IAsyncOperation<​TResult>//​ objects into .NET //​System.Threading.Tasks.Task//​ (or //​System.Threading.Tasks.Task<​TResult>//​) objects.
 +To handle cancellation and progress updates, call one of the //AsTask// extension methods.
 +
 +<code csharp>
 +public static class WindowsRuntimeSystemExtensions // defined in System
 +{
 +    public static Task AsTask(this IAsyncAction source, CancellationToken cancellationToken);​
 +    public static Task AsTask<​TProgress>​(this IAsyncActionWithProgress<​TProgress>​ source, CancellationToken cancellationToken,​ IProgress<​TProgress>​ progress);
 +    public static Task<​TResult>​ AsTask<​TResult>​(this IAsyncOperation<​TResult>​ source, CancellationToken cancellationToken);​
 +    public static Task<​TResult>​ AsTask<​TResult,​ TProgress>​(this IAsyncOperationWithProgress<​TResult,​ TProgress>​ source, CancellationToken cancellationToken,​ IProgress<​TProgress>​ progress);
 +
 +    public static IAsyncAction AsAsyncAction(this Task source);
 +    public static IAsyncOperation<​TResult>​ AsAsyncOperation<​TResult>​(this Task<​TResult>​ source);
 +    ...
 +}
 +</​code>​
  
 Example: Write a line of text to a file: Example: Write a line of text to a file:
notes/uwp/iasync_interfaces.txt ยท Last modified: 2019/01/06 by leszek