c# - Async methods in unhandled exception handler in Universal Apps -


i'm writing handler unhandled exception event dump exception file , send server further analyze. show crash had happen. subscribed event. in method called function handling errors. however, when wrote in method code:

public async task handleexception(exception exception) {     var dialog = new messagedialog(exception.message, "exception occurred");     await dialog.showasync(); } 

and run in debug mode visual studio shows visual studio just-in-time debugger. in first place thought problem i'm trying show message box when gui thread dying. changed function to:

public async task handlemanagedexception(exception  {      await filestorage.writetofileasync("somefile.txt", exception.tostring()); } 

where function filestorage.writetofileasync looks like:

public async task writetofileasync(string filename, string data) {     var file = await this.storagefolder.createfileasync(filename, creationcollisionoption.replaceexisting);     await fileio.writetextasync(file, data); } 

in debugger mode, when put breakpoint on await fileio.writetextasync(file,data) stops there. after pressing continue button, same window shows in previous code. running when called xaml events. i've searched google , stackoverflow async methods in unhandled exception handler, there nothing related problem.

my questions are: reason error? possible use async methods in unhandled exception handler?

update: thank answers far. second question answered myself after close debugging code. turned out possible , function working intended.

to avoid visual studio just-in-time debugger dialog, can set unhandledexceptioneventargs.handled property true following:

public app() {     this.initializecomponent();     this.suspending += onsuspending;     this.unhandledexception += app_unhandledexception; }  private async void app_unhandledexception(object sender, unhandledexceptioneventargs e) {     e.handled = true;     await handleexception(e.exception); }  public async task handleexception(exception exception) {     var dialog = new messagedialog(exception.message, "exception occurred");     await dialog.showasync(); } 

in debug mode, warning , just-in-time debugger dialog because there following codes in app.g.i.cs:

#if debug && !disable_xaml_generated_break_on_unhandled_exception unhandledexception += (sender, e) => {     if (global::system.diagnostics.debugger.isattached)          global::system.diagnostics.debugger.break(); }; #endif 

these codes generated automatically when build project. , these codes, can find while in debug model, if there unhandled exception, call debugger.break method , result in warning , just-in-time debugger dialog.

setting e.handled = true; in our handler can avoid this. more info, can check similar question: how try/catch exceptions. however, can see happen in debug mode, in release model, code should work well.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -