-2

Error in line: await instance.CreateFile(); The await operator can only be used within an async method. Consider marking this method with the async modifier and changing its return type to Task It's my understanding that the method is already asynchronous, I'm not sure what I am doing wrong. This is on a UWP app in VS 2015.

public sealed partial class MainPage : Page
{
    private List<Customer> Customers;
    public MainPage()
    {
        this.InitializeComponent();
        CustomerDataAccessLayer instance = new CustomerDataAccessLayer();
        await instance.CreateFile();
}
}

public class CustomerDataAccessLayer
{
    public StorageFile mfile;
    public async Task CreateFile()
    {
        mfile = await ApplicationData.Current.LocalFolder.GetFileAsync("Customers.xml");
        if (mfile == null)
        {
            mfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("Customers.xml", CreationCollisionOption.ReplaceExisting);
        }
        else
        {
        }
        return;

    }
 }
  • 6
    That's a constructor, and no, it's not marked `async` (nor can it be). You can tell from the lack of the `async` keyword in it's declaration. – Servy Oct 26 '15 at 17:37
  • But it doesn't have an async so it's not..? – Liam Oct 26 '15 at 17:38
  • 1
    You can't use async/await in constructors. The `async` does not follow through the method chain, each individual function that uses `await` must be marked `async`, however as @Servy said, you can't mark the constructor `async` [and there are good reasons why](http://stackoverflow.com/questions/8145479/can-constructors-be-async). – Ron Beyer Oct 26 '15 at 17:40
  • So a method cannot be marked async? I do have the keyword in public async Task CreateFile() Do I have to change it into a class? – Vlad Fuentes Oct 26 '15 at 17:45
  • There is a difference between methods and constructors. Constructors are not normal methods (they have no explicit return type, its implied) and they cannot be decorated with `async`. Methods can be decorated with it, basically your problem boils down to using `await` inside the constructor, which is not allowed. Instead of using `await`, use `instance.CreateFile.Wait()` (no `await`), which allows you to call an async method without await synchronously (will block). – Ron Beyer Oct 26 '15 at 17:50
  • I ended up using Ron's solution. So much to learn! Thanks everyone – Vlad Fuentes Oct 27 '15 at 20:03

2 Answers2

0

As others have noted, the error message is complaining that the calling method is not async, but constructors cannot be marked async.

The core problem is that your UI cannot load asynchronously - it must load immediately. I recommend initially loading some kind of "Loading..." state and also starting an asynchronous operation. Then, when that operation completes, update the UI to display the data.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

To start an asynchronous action from a synchronous function (like a constructor), you can use a Task.

Task.Run(async () =>
{
    //put your async code here
});

To await this Task, you have to use some more code.

Task t = new Task(() => { /*Your code*/ });
t.Wait();
3dz9j56
  • 112
  • 7