c# - await async write smoothly to ui but still freeze it -


i doing interpreter-ish. ui wpf app. on button click, launch long task has write ui. have done similar things, in past, background worker. realised background worker obsolete , async way go ( less noisy & cleaner etc. ).

i've got long task smoothly write text screen while looping, screen still 'frozen' i.e can't click on buttons etc.

if add sleep of 30 ms @ end of task.run, ui gets responsive , object events working. sleeps seems make slower 30 ms.

private void analyzebutton_click( object sender, routedeventargs e ) {     var flowmanager = new interpreterflowmanager(){ ... }     flowmanager.initialize();     flowmanager.continueflow(); } ....  public class interpreterflowmanager {     async public void continueflow()     {         string linebuffer = "";          string lasttextblockstring = "";         while (...)         {             lasttextblockstring = "";             bool endloop = false;              await task.run(() =>              {                 if (...)                 {                     ...                     if (...)                     {                         switch (...)                         {                             case x:                                 lasttextblockstring = "\r\nentrer un nombre :";                                 endloop = true;                                 break;                             case y:                                 lasttextblockstring = "\r\n" + consoleinfo.message;                                 break;                         }                     }                 }                 else                     lineindex++;                            *********************************                            small sleep here make ui responsive.                           ************************************             });              //access ui textbox             var lasttextblock = (interactivewindow.children.cast<textbox>().where(c => c.gettype() == typeof(textbox) && c.isreadonly)).last();             //sucessfully append             lasttextblock.text += lasttextblockstring;             output.text += _tmpresultstring + "\r\n";              _tmpresultstring.clear();              if (endloop)              {                 textbox input = new textbox();                 input.borderbrush = brushes.darkred;                 input.keyup += input_keyup; // further stuff                  interactivewindow.children.add(input);                 return;              }         }     } } 

if you're familiar backgroundworker, there's pretty straightforward translation async world:

  1. replace each backgroundworker instance single task.run call.
  2. replace progresschanged event handler progress<t> (described on msdn).
  3. replace runworkercompleted event code after await task.run(...).

your code looks it's starting separate task.run within while loop. if code in each task.run executes quickly, not give ui opportunity responsive. consider moving while loop within task.run delegate , using iprogress<t>/progress<t> progress updates.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -