c# - Code not executing before thread sleep? -


i this:

clear(); coinrefundcomplete.visible = true; state = 0;  system.threading.thread.sleep(4000); clear();  greeting.visible = true; rate.visible = true; refundticket.visible = true; currenttime.visible = true; 

i expect coinrefundcomplete text (it label) appear 4 seconds, cleared method defined clear(), , other stuff happens. instead after clear form first clear(), form blank 4 seconds, finishes properly.

although putting gui thread sleep never desirable allowing gui thread refresh control state before going sleep show changes want. call control.update or control.refresh after making visible , before going sleep gui thread able show changes before goes sleep.

clear(); coinrefundcomplete.visible = true; label1.update(); state = 0;     system.threading.thread.sleep(4000); clear(); 

you should carefull while using thread.sleep, in case gui thread , gui irresponsive time sleep. knowing reason why want block thread bring other better suggestion.

edit

you can use other thread adding delay without blocking gui thread. if can use framework 4.5 can use async / await construct or read article using async/await without .net framework 4.5 .

private async void testasyncawaitdely_click(object sender, eventargs e) {      clear();      coinrefundcomplete.visible = true;      state = 0;      await task.delay(4000);      clear();      //your code } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -