Reputation: 62
I'm trying to delay my code by 5 seconds this is what I'm using
System.Threading.Thread.Sleep(5000);
However this freeze my app completly I wonder if there's a better way
Upvotes: 0
Views: 515
Reputation: 60
There are a lot of ways to achieve that.
One of them is to create a new thread so the main thread will keep going when you freeze the new one.
using System.Threading;
Console.WriteLine("Hello from the main thread");
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
Console.WriteLine("Background thread started");
Thread.Sleep(5000);
Console.WriteLine("Waited 5 sec");
}).Start();
Console.WriteLine("The main thread is still running while the other one is waiting");
Upvotes: 1