How to stop program for a time without System.Threading.Thread.Sleep. c#? -


this question has answer here:

system.threading.thread.sleep stop time application, doesn't work need.

this code:

buttonship.enabled = false; saving(); setproperties(); loading(); showlabels(); system.threading.thread.sleep(3000); buttonship.enabled = true; 

i can click buttonship after first click. when program shows button disabled, enabled.

my question is, how can stop program 3 sec?

without a good, minimal, complete code example illustrates scenario, it's hard know sure want do.

but, seems me want execute work not in ui thread. allow change in button's enabled state visible user, allow rest of ui remain responsive while work occurs.

a basic way accomplish change code looks more this:

async void buttonship_click(object sender, eventargs e) {     buttonship.enabled = false;     await task.run(() =>     {         saving();         setproperties();         loading();         showlabels();     }      // wait 3 more seconds before re-enabling button:     await task.delay(3000);      buttonship.enabled = true; } 

this cause of other code execute in different thread ui thread. when task started, buttonship_click() method return without having executed last statement. statements in task execute in different thread. when task has completed, control return buttonship.click() method; begin executing @ statement following await statement, i.e. 1 re-enables button.

unfortunately, original code example extremely vague. there possibility @ least of work in task should updating ui. if that's case, there ways accomplish that, including simple use of control.invoke() execute ui-related code on ui thread belongs, or use progress<t> class accomplish same via iprogress<t> interface.

if want more advice along lines, recommend trying above, research other issues may arise (if any), , if unable solve them, post new question regarding new issues, being sure include code example.


edit:

per comment above, quoted here:

the program can executed 100 milliseconds, in case, button must active after 3 seconds of 100 milliseconds. if program executed 5 seconds, in case, button must active after 8 seconds

i take understanding no matter how long other task takes execute, want button not re-enabled until 3 seconds later. have updated code example above accomplish that.


Comments