c# - UI freezes when using async await -
c# - UI freezes when using async await -
i have troubles create ui work using async method. here part of code
private async void btndooutput_click(object sender, routedeventargs e) { /* initiliaze */ groupboxconfiguration.isenabled = false; var progressindicator = new progress<int>(); progressindicator.progresschanged += (s,value) => { progressexport.value = (double)value; labelpercentage.content = "export in progress : " + value + " %"; }; /* work */ switch (something) { case 1: await method(input, output, options, progressindicator); break; default: break; } /* finalization */ groupboxconfiguration.isenabled = true; }
the method
public async static task<string> method(string input, string output, string options, iprogress<int> progress) { while(something) { //operations on input , output if (progress != null) { progress.report(percentage); } } }
when click on button, ui freezes, groupbox still enabled, progress not shown until end.
i think misunderstanding how async
/ await
works. of code still running on ui thread because don't tell otherwise. means await
on method
pointless because it's going run synchronously anyway.
the purpose of async
/await
allow calling code chance go on processing until hits part of code requires result of awaitable task. in example, need alter method
body homecoming awaitable task
public task method(string input, string output, string options, iprogress<int> progress) { homecoming task.run(() => { while(something) { //operations on input , output if (progress != null) { progress.report(percentage); } } }); }
c# wpf user-interface asynchronous async-await
Comments
Post a Comment