.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
Post a Comment