await in override void loadstate problem

Answered await in override void loadstate problem

  • Monday, August 13, 2012 5:07 PM
     
     

    Hi,

    I need to Deserialize (using await) some application settings in the override method LoadState of a Metro App: 

    protected override void LoadState(...)

    The method is not awaitable because it returns void. I cant' control loading of data.  

    With the use of a semaphore I have find a solution that seems work to me:

    protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
        LoadStateAsync();           // this continues execution without wait
    }
    private async Task LoadStateAsync()
    {
        app.PlaySemaphore.WaitOne();   // semaphore defined in App class (here app)
        StorageFile file = null;
        file = await   ApplicationData.Current.LocalFolder.GetFileAsync  (App.FILENAME_SETTINGS);
        app.PlaySettings = await Settings.LoadAsync(file);   // an async method for Deserialize
        app.PlaySemaphore.Release();
    }
    ...... far away in code
        app.PlaySemaphore.WaitOne();
        app.PlaySemaphore.Release();
    ...... continue with execution using app.PlaySettings


    ...)

    Someone have just try it ? Have you find contraindications ?

All Replies

  • Tuesday, August 14, 2012 5:54 AM
     
     Answered

    I have tried the code in a similar context. It code does not work. (It blocks UI thread)