Async property in c#

后端 未结 4 1528
礼貌的吻别
礼貌的吻别 2020-12-17 01:14

In my windows 8 application there is a global class where there are a few static properties like:

public class EnvironmentEx
{
     public static Us         


        
4条回答
  •  我在风中等你
    2020-12-17 01:54

    I suggest you use asynchronous lazy initialization.

    public static readonly AsyncLazy AppRootFolder =
        new AsyncLazy(() =>
        {
          return KnownFolders.DocumentsLibrary                    
              .CreateFolderAsync("theApp", CreationCollisionOption.OpenIfExists)
              .AsTask();
        });
    

    You can then await it directly:

    var rootFolder = await EnvironmentEx.AppRootFolder;
    

提交回复
热议问题