Search code examples
c#asynchronouswindows-store-appswindows-store

Windows Store Application - Synchronously waiting for an async operation


I would like to use this solution for synchronously waiting for an async operation.

 public static ConfiguredTaskAwaitable<T> StartAsTask<T> (
    this IAsyncOperation<T> self, bool continueOnContext)
{
    if (self == null)
        throw new ArgumentNullException("self");

    self.AsTask().ConfigureAwait(continueOnContext);
}

When I call this method, I get error message "Windows.Foundation.IAsyncAction" does not contain a definition for "StartAsTask" and no extension method.

await FileIO.WriteTextAsync(sampleFile, sampleString).StartAsTask (false);

Can anyone give me any directions?


Solution

  • This is not a solution for doing anything synchronously, it's simply how to avoid calling back to the UI thread when you don't need to. The problem with your actual code is that you've defined an extension for IAsyncOperation<T>, not IAsyncAction.