Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, December 21, 2012 10:18 PM
I want an async C# equivalent to JavaScript's setTimeout function. I came up with this and it seems to work. Can anyone see any potential issues with the approach?
usage:
await SetTimeout(1000);
public static Task SetTimeout(int ms)
{
Timer t = null;
Task ret = new Task(() => { });
var callback = new TimerCallback((o) =>
{
ret.RunSynchronously();
t.Dispose();
});
t = new Timer(callback, null, ms, Timeout.Infinite);
return ret;
}
All replies (1)
Saturday, December 22, 2012 3:55 AM âś…Answered | 2 votes
You can do this via Task.Delay directly: http://msdn.microsoft.com/en-us/library/hh460377.aspx
Just call:
await Task.Delay(1000);
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".