The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
Want to run "ReadFileAsync" in synchronization.way RRS feed

  • Question

  • Want to run "ReadFileAsync" in synchronization...i am calling this method in constructor and i want to load this in constructor only.. used RunSynchronously  but didnt help..so how can i run this method as a synchronization

    //
        // Summary:
        //     Shared implementation of ObjectStorageHelper.
        public abstract class BaseObjectStorageHelper : IObjectStorageHelper
        {
            protected BaseObjectStorageHelper();
     
            //
            // Summary:
            //     Gets or sets the settings container.
            protected ApplicationDataContainer Settings { getset; }
            //
            // Summary:
            //     Gets or sets the storage folder.
            protected StorageFolder Folder { getset; }
     
            //
            // Summary:
            //     Determines whether a file already exists.
            //
            // Parameters:
            //   filePath:
            //     Key of the file (that contains object)
            //
            // Returns:
            //     true if the file exists; otherwise, false.
            public Task<boolFileExistsAsync(string filePath);
            //
            // Summary:
            //     Determines whether a setting already exists.
            //
            // Parameters:
            //   key:
            //     Key of the setting (that contains object)
            //
            // Returns:
            //     true if the setting exists; otherwise, false.
            public bool KeyExists(string key);
            //
            // Summary:
            //     Determines whether a setting already exists in composite.
            //
            // Parameters:
            //   compositeKey:
            //     Key of the composite (that contains settings)
            //
            //   key:
            //     Key of the setting (that contains object)
            //
            // Returns:
            //     true if the setting exists; otherwise, false.
            public bool KeyExists(string compositeKeystring key);
            //
            // Summary:
            //     Retrieves a single item by its key.
            //
            // Parameters:
            //   key:
            //     Key of the object
            //
            //   default:
            //     Default value of the object
            //
            // Type parameters:
            //   T:
            //     Type of object retrieved
            //
            // Returns:
            //     The T object
            public T Read<T>(string keyT @default = default);
            //
            // Summary:
            //     Retrieves a single item by its key in composite.
            //
            // Parameters:
            //   compositeKey:
            //     Key of the composite (that contains settings)
            //
            //   key:
            //     Key of the object
            //
            //   default:
            //     Default value of the object
            //
            // Type parameters:
            //   T:
            //     Type of object retrieved
            //
            // Returns:
            //     The T object
            public T Read<T>(string compositeKeystring keyT @default = default);
            //
            // Summary:
            //     Retrieves an object from a file.
            //
            // Parameters:
            //   filePath:
            //     Path to the file that contains the object
            //
            //   default:
            //     Default value of the object
            //
            // Type parameters:
            //   T:
            //     Type of object retrieved
            //
            // Returns:
            //     Waiting task until completion with the object in the file
            [AsyncStateMachine(typeof(BaseObjectStorageHelper.<ReadFileAsync>d__16<>))]
            public Task<TReadFileAsync<T>(string filePathT @default = default);
            //
            // Summary:
            //     Saves a single item by its key. This method should be considered for objects
            //     that do not exceed 8k bytes during the lifetime of the application (refers to
            //     Microsoft.Toolkit.Uwp.Helpers.BaseObjectStorageHelper.SaveFileAsync``1(System.String,``0)
            //     for complex/large objects).
            //
            // Parameters:
            //   key:
            //     Key of the value saved
            //
            //   value:
            //     Object to save
            //
            // Type parameters:
            //   T:
            //     Type of object saved
            public void Save<T>(string keyT value);
            //
            // Summary:
            //     Saves a group of items by its key in a composite. This method should be considered
            //     for objects that do not exceed 8k bytes during the lifetime of the application
            //     (refers to Microsoft.Toolkit.Uwp.Helpers.BaseObjectStorageHelper.SaveFileAsync``1(System.String,``0)
            //     for complex/large objects) and for groups of settings which need to be treated
            //     in an atomic way.
            //
            // Parameters:
            //   compositeKey:
            //     Key of the composite (that contains settings)
            //
            //   values:
            //     Objects to save
            //
            // Type parameters:
            //   T:
            //     Type of object saved
            public void Save<T>(string compositeKeyIDictionary<stringTvalues);
            //
            // Summary:
            //     Saves an object inside a file. There is no limitation to use this method (refers
            //     to Microsoft.Toolkit.Uwp.Helpers.BaseObjectStorageHelper.Save``1(System.String,``0)
            //     method for simple objects).
            //
            // Parameters:
            //   filePath:
            //     Path to the file that will contain the object
            //
            //   value:
            //     Object to save
            //
            // Type parameters:
            //   T:
            //     Type of object saved
            //
            // Returns:
            //     The Windows.Storage.StorageFile where the object was saved
            public Task<StorageFileSaveFileAsync<T>(string filePathT value);

    Monday, July 1, 2019 12:52 PM

All replies

  • Hi,

    If you want to invoke  ReadFileAsync  in  constructor  then get  result, you could package  ReadFileAsync method with Action parameter, For detail steps please refer the following code.

    private async void ReadFile(Action<MyLargeObject> resault)
    {
        var localObjectStorageHelper = new LocalObjectStorageHelper();    
        string keyLargeObject = "large";
    
      /*  var o = new MyLargeObject
        {
            MyContent = "heee",
            MyContents = new List<string>() { "nico", "barry", "roy" },
            MyObjects = new List<MyLargeObject>()
    
        };
    
        await localObjectStorageHelper.SaveFileAsync(keyLargeObject, o);  */
        resault(await localObjectStorageHelper.ReadFileAsync<MyLargeObject>(keyLargeObject));
       
    }

    Usage

    public HomePage()
    {
        this.InitializeComponent();
        ReadFile((res) =>
        {
            System.Diagnostics.Debug.WriteLine(res.ToString());
    
        });
    }

    Best regards,

    Nico




    Monday, July 1, 2019 2:40 PM