.net - Insert delay/wait in code c# -


    public void openupform(object sender, eventargs e)     {         if (forms.count == numberoftimes)         {             forms.foreach(f =>             {                 f.close();                  f.dispose();              });             forms.clear();              //need delay here             return;         }         forms.add(new form1());         forms.last().show();     } 

hello have code, need add delay after forms.clear(); im new coding couldnt figure out. have tryed task.delay , thread.sleep locks user interface. possible add delay dosent lock application? thank you.

you can mark method async , use this:

await task.delay(2000); 

will not block ui thread

public async void openupform(object sender, eventargs e) {     if (forms.count == numberoftimes)     {         forms.foreach(f =>         {             f.close();              f.dispose();          });         forms.clear();          await task.delay(2000);         return;     }     forms.add(new form1());     forms.last().show(); } 

this behave so.

  • creates new task runs completion after 2 seconds
  • ui thread bubbled continue executing/processing other events
  • once 2 seconds ui thread returns , resumes executing async method after await

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -