Async property in c#
I suggest you use asynchronous lazy initialization.
public static readonly AsyncLazy<StorageFolder> AppRootFolder =
new AsyncLazy<StorageFolder>(() =>
{
return KnownFolders.DocumentsLibrary
.CreateFolderAsync("theApp", CreationCollisionOption.OpenIfExists)
.AsTask();
});
You can then await
it directly:
var rootFolder = await EnvironmentEx.AppRootFolder;
Good solution: Don't make a property. Make an async method.
"I hate await, how can I make everything synchronous?" solution: How to call asynchronous method from synchronous method in C#?
use the await keyword
public async static StorageFolder GetAppRootFolder()
{
return await ApplicationData
.LocalFolder
.CreateFolderAsync("folderName");
}
and in your code
var myRootFolder = await StaticClass.GetAppRootFolder(); // this is a synchronous call as we are calling await immediately and will return the StorageFolder.