简体   繁体   中英

Writing file asynchronously in unhandled exception handler

I'm writing handler for Unhandled Exception event which we will dump an exception to a file and then send to a server for further analyze. For now I want to save exception to a file and show it in the next run. From the handler I'm calling a function, which has such implementation:

public async Task WriteDataAsync(string filename, string data)
{
    var file = await this.storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteTextAsync(file, data);
}

The problem is that if file didn't existed it is created but the exception data isn't saved to the file. However if the file existed the data is saved properly. This function works when it is not used in handling unhandled exception.
I have two questions in this matter.

  1. What is the reason for such behavior in that context. I guess it has to do with way the async/await methods works, which is ok for normal situations, but has problems in handling exceptions in this context.
  2. Are there other solutions to write to file asynchronously in that context? I have one working solution, which is to make run function synchronously in this case.

     public void WriteDataAsync(string filename, string data) { var fileTask = this.storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting).AsTask(); var writingToFileTask = FileIO.WriteTextAsync(fileTask.Result, data).AsTask(); writingToFileTask.Wait(); } 

    I had also an idea to create file before writing. However, the data was not written to file again.

I think you can use FileMode.OpenOrCreate:

    public async Task WriteDataAsync(string fileName, string data)
    {
        byte[] text = Encoding.Unicode.GetBytes(data);

        using (var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
        {
            await stream.WriteAsync(text, 0, text.Length);
        };
    }

What is the reason for such behavior in that context. I guess it has to do with way the async/await methods works.

Your guess is right. When we call the await method, it returns control to the caller of the async method. In this context, the control will finally return to Windows Runtime. And normally after the UnhandledException event is fired, the Windows Runtime will terminate the app. So it's possible that the app is terminated before the asynchronous operations has completed its work. And this could lead to the behavior you've seen.

Are there other solutions to write to file asynchronously in that context? I have one working solution, which is to make run function synchronously in this case.

For this scenario, usually we need a deferral for the asynchronous operation like SuspendingDeferral or BackgroundTaskDeferral . However there is no such thing in UnhandledException event. Your solution is correct in this case, we can make these operations synchronously to avoid this issue. You can refer to this blog: Strategies for Handling Errors in your Windows Store Apps to handle unhandled exception. It also uses this solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM