.NET by Patrik

Thread.Sleep (synchronous)

The Thread.Sleep Method Suspends the current thread for the specified amount of time.

  • Thread.Sleep is a synchronous method that blocks the current thread for the specified amount of time.
  • It's generally used in non-async scenarios or when dealing with multi-threading where you explicitly want to pause the execution of the current thread.
  • It can introduce responsiveness issues, especially in GUI applications, as it will freeze the UI during the sleep period.

Example:

void MyMethod()
{
    // Do something before the delay
    Thread.Sleep(1000); // Sleep for 1000 milliseconds (1 second)
    // Do something after the delay
}

Comments