Search code examples
c#.netmultithreadinguwp.net-native

UWP CreateFileAsync thread exits when compiled in release mode with Compile with .NET Native tool chain checked


I have a UWP app and I am trying to save a config file using the below code. This works fine with Compile with .NET Native tool chain unchecked and in debug mode. However once Compile with .NET Native tool chain is checked the thread exits when trying to create the file. How do I get around this?

public async void SaveConfig()
{
    // serialize JSON to a string
    string json = JsonConvert.SerializeObject(uris);

    // write string to a file
    var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("config.json", CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteTextAsync(file, json);
}

Solution

  • The problem is most likely to be in the async void method which isn't awaited to be executed till the end. If this is the case, the solution would be to change the method header

    public async Task SaveConfigAsync()
    

    and invoke the method like this

    await SaveConfigAsync();