I know there is Thread.Sleep and System.Windows.Forms.Timer and Monitor.Wait in C# and Windows Forms. I just can’t seem to be able to figure out how to wait for X seconds and then do something else – without locking the thread.
I have a form with a button. On button click a timer shall start and wait for 5 seconds. After these 5 seconds some other control on the form is colored green. When using Thread.Sleep, the whole application would become unresponsive for 5 seconds – so how do I just “do something after 5 seconds”?
(transcribed from Ben as comment)
…and disable the timer (IsEnabled=false) before doing your work in oder to suppress a second.
The Tick event may be executed on another thread that cannot modify your gui, you can catch this:
Just for completeness: with async/await, one can delay execute something very easy (one shot, never repeat the invocation):
For more, see "async and await" in MSDN.
more completeness:
Depending on your Framework, there is a good chance you will have
DispatcherTimerclass that can handle the invocation internally (WPF-variants). (finde details in ms docs)