Winforms equivalent of javascript setTimeout
I know this is an old question but an alternative solution would be to use Task.Delay(delay).ContinueWith((task) => { /* Code */ });
.
Thread.Sleep vs Task.Delay?
or there is await Task.Delay(delay);
https://social.msdn.microsoft.com/Forums/vstudio/en-US/345f0402-3af0-4f96-a501-073674883ba3/building-an-async-settimeout-function?forum=csharpgeneral
You can use a System.Timers.Timer: set AutoReset to false and use Start/Stop methods and create a handler for the Elapsed event.
Here's an example implementation in vb.net
:
Public Sub SetTimeout(act As Action, timeout as Integer)
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer(1)
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, Sub () act
aTimer.AutoReset = False
aTimer.Enabled = True
End Sub
public void setTimeout(Action TheAction, int Timeout)
{
Thread t = new Thread(
() =>
{
Thread.Sleep(Timeout);
TheAction.Invoke();
}
);
t.Start();
}