Answers for "how can i creat async function with thread.sleep in c#"

C#
4

c# async sleep

// Async
await Task.Delay(1000); //when you want a logical delay without blocking the current thread
// Not Async
Thread.Sleep(1000) //when you want to block the current thread.
Posted by: Guest on August-22-2020
2

C# Thread Sleep vs Task Delay

// Thread Sleep vs Task Delay
int delayInSec = 3;

// Using Thread Sleep
Thread.Sleep(delayInSec * 1000);

// Using Task Delay
Task.Delay(delayInSec * 1000).Wait();
Posted by: Guest on November-08-2021
0

c# async sleep

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    label1.Text = "Test";
    await Task.Delay(2000);
    label1.Text = "";
}
Posted by: Guest on March-02-2022

C# Answers by Framework

Browse Popular Code Answers by Language