. Advertisement .
..3..
. Advertisement .
..4..
Wondering how to add delay timer in C#? You have ITtutoria support team behind your back. This article will introduce two different methods for delay insert in C#.
How to Add Delay Timer in C#
Method 1. Use Thread.Sleep()
// Will delay for four seconds
Thread.Sleep(4000);
Thread.Sleep()
is among the simplest methods to add delays in your C# code. Still, your main threat will get hung during the delay’s duration, which is why this approach only works best with console applications. Suppose you have no problem with that, we will analyze a more thorough example:
using System;
using System.Threading;
class Program
{
static void Main()
{
Console.WriteLine($"Delay starts at {DateTime.Now}");
// Will delay for four seconds
var milliseconds = 4000;
Thread.Sleep(milliseconds);
Console.WriteLine($"Finishes delay at {DateTime.Now}");
}
}
Output:
Delay starts at 13/10/2019 11:58:38
Finishes delay at 13/11/2020 11:58:42
Remember to execute the codes carefully. One of the most popular mistakes with Thread.Sleep()
usage is that many programmer forget to add “Using”, leading to this error message:
error CS0103 C# The name 'Thread' does not exist in the current context
To fix this problem, you need to add the line “using.System.Threading
” at the file’s top, just like what we introduced in the previous example.
Next, remember that Thread.Sleep()
assigns milliseconds as one of its main arguments. Hence, for an addition of 4 seconds, it’s a must to pass the “4000” value. You can make the intent clearer by using this timespan:
Thread.Sleep(TimeSpan.FromSeconds(4));
Still, previous Thread.Sleep()
versions did not take TimeSpans, which means your mileage might vary.
Method 2. Use Task.Delay() to Avoid Blocking The Main Thread
// Will delay for 4 seconds
await Task.Delay(4000);
There are asynchronous Thread.Sleep
versions named “Task.Delay
“. For more info on asynchronous calls’ functions in C#, you can always turn to official docs.
Anyways, the major concept is to kickstart new tasks that operate your delay within the background context. Here is an illustration:
using System;
using System.Threading.Tasks;
class Program
{
async static Task Main()
{
Console.WriteLine($"Delay starts at {DateTime.Now}");
// Will delay for 4 seconds
await Task.Delay(4000);
Console.WriteLine($"Finishes delay at {DateTime.Now}");
}
}
Output:
Delay starts at 13/10/2019 12:22:08
Finishes delay at 13/10/2019 12:22:12
Again, do not forget to add “Using”. Otherwise, you will only receive this error message:
CS0246 C# The types or namespace name ‘Task’ can not be found (are you missing using directive or assembly references?)
Thus, always make sure you add the directive “using System.Threading.Tasks
” just like what we illustrated to avoid such errors.
Conclusion
Our article has presented two effective approaches that help you add delay timer in C#. For more guidance on related C# issues (such as declaring global variables), feel free to browse this website.
Leave a comment