25

I'm trying to iterate all files in a directory using GetFilesAsync, but every time I call the GetResults method, it throws an exception that says

System.InvalidOperationException: A method was called at an unexpected time

The code is simply

var files = myStorageFolder.GetFilesAsync(); //runs fine
var results = files.GetResults(); //throws the exception

I'm new to Win 8 dev so I might be missing something obvious.

Edit (solved) I'm running my console application, but now that the program runs async, the files.GetResult() method no longer exists.

static void Main(string[] args)
{
   var files = GetFiles(myStorageFolder);
   var results = files.GetAwaiter().GetResults();//Need to add GetAwaiter()
}

static async Task GetFiles(StorageFolder sf)
{
   await sf.GetFilesAsync();
}
XSL
  • 2,965
  • 7
  • 38
  • 61

4 Answers4

41

If you don't want to use the asynckeyword (in my case, the code was inside a property, so asyncwasn't an option), you can use a TaskAwaiter instead, by chaining these two methods:

var folder = Package.Current.InstalledLocation.GetFolderAsync("folderName").GetAwaiter().GetResult();

This won't throw a InvalidOperationException nor cause a deadlock.

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • 1
    Do I need any `Using` statements to use the `GetAwaiter` method? – JKennedy Nov 24 '16 at 09:45
  • 1
    @user1 I can't remember, this is from 2 years ago. But looking through the docs, this is the extension method you need: [`WindowsRuntimeSystemExtensions.GetAwaiter`](https://msdn.microsoft.com/en-us/library/hh582011(v=vs.110).aspx). It's defined in the `System` namespace, so you probably have it in scope already – dcastro Nov 24 '16 at 11:14
  • 7
    I noticed the `GetAwaiter()` was on a `Task` and I was trying to use it on an `IAwaitableOperation` so I had to call `AsTask` then `Results`, damn `UWP`. Thanks though! – JKennedy Nov 24 '16 at 11:17
  • This also solves the issue when in an `if` statement as well. I was receiving the error when I just ran the application, but not if I placed a breakpoint just before the `if` statement and used Step Into to run through the `if` statement. `if(ApplicationData.Current.LocalFolder.TryGetItemAsync("savedFavorites.xml").GetAwaiter().GetResult() == null)` – Keven M Mar 14 '19 at 16:15
13

You need to wait for the async method to complete. So you could use the new await as one option:

var files = await myStorageFolder.GetFilesAsync();

You might want to check the documentation on dealing with async methods here.

AndrewS
  • 6,054
  • 24
  • 31
1

If you are using an variation of Task like Windows.Foundation.IAsyncOperation the code can be like this:

var file = StorageFile.GetFileFromPathAsync(protocolArgs.Uri.LocalPath).AsTask().Result;
Murilo Maciel Curti
  • 2,677
  • 1
  • 21
  • 26
0

you should await the var files = myStorageFolder.GetFilesAsync(); as the operation might still be running when you get to next instruction var results = files.GetResults(); //throws the exception

var files = await myStorageFolder.GetFilesAsync(); //runs fine
var results = files.GetResults(); //this will run when call above returns
Mayank
  • 8,777
  • 4
  • 35
  • 60