Difference between Thread.Sleep and Task.Delay

4

I'm developing client / server communication, and using Task for asynchronous communication.

Previously, I had already made another communication where I used Thread , works without problems and consumes little processing.

In this now with Task I had a high processing, and seemed to not happen the Delay between iterations of while . I have decided to change all Task.Delay to Thread.Sleep .

And the result was satisfactory. It started to have the delay at each iteration and the CPU consumption remained low.

Here's the question: What is the difference between Task.Delay() and Thread.Sleep()

  

Code snippet where TcpListener accepts connections   (This snippet is within the execution of a Task ):

while  (_running)
{
    if (server.Pending())
    {
        TcpClient client = server.AcceptTcpClient();
        string nIP = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
        ChatServerClient clie = new ChatServerClient(++_idControl, client, this._log);
        _clients.Add(clie);
        ClientConnected(new ClientChatEventArgs() { Client = clie });
        clie.OnClientStop += clie_OnClientStop;
        clie.StartClient();
        clie.Enviar.Enqueue("Servidor Conectado.");
    }
    else
    {
        Thread.Sleep(2000); //Funciona, baixo CPU e espera o tempo
        //Task.Delay(2000); //Não funciona, alto CPU e não espera o tempo
    }
}
    
asked by anonymous 07.12.2017 / 20:08

1 answer

3

Considering the following example

private void button1_Click(object sender, EventArgs e)
{
    Thread.Sleep(10000);
}

private async void button2_ClickAsync(object sender, EventArgs e)
{
    await Task.Delay(10000);
}

public static void Sleep(int millisecondsTimeout)

This is the classic way to suspend execution. This method will suspend the current thread until the amount of time has elapsed. When you call Thread.Sleep the way above, there is nothing you can do to abort this except wait until the time elapses or restart the application. This is because Thread.Sleep suspends the thread that is making the call. And because I'm calling Thread.Sleep in my button event handler, the UI stays frozen until the specified time expires.

public static Task Delay(int millisecondsDelay)

Task.Delay acts in a very different way than Thread.Sleep . Basically, Task.Delay will create a task that will be completed after a delay. Task.Delay is not blocking the user interface and it will continue to respond.

Behind the scenes is a stopwatch until the specified time. Once the timer controls the delay, we can cancel the delay at any time simply by stopping the timer. To cancel the execution of Task.Delay you have to pass over the CancellationToken cancellationToken parameter. You can use the example below.

CancellationTokenSource tokenSource = new CancellationTokenSource();
private void button1_Click(object sender, EventArgs e)
{
    Thread.Sleep(10000);
}

private async void button2_ClickAsync(object sender, EventArgs e)
{
    await Task.Delay(10000, tokenSource.Token);
}

private void button3_Click(object sender, EventArgs e)
{
    tokenSource.Cancel();
}

An important detail to highlight is the implementation of async in Task.Delay. As per the documentation Asynchrony is essential for activities that are potentially being blocked, as well as when your application accesses the Web. Access to a Web resource is sometimes slow or delayed. If such activity is blocked within a synchronous process, the entire application must wait. In an asynchronous process, the application can proceed with another job that does not depend on the Web resource until the task that caused the lock to end.

Reference:

Visual C # : Thread.Sleep vs. Task.Delay . Available at: link . Accessed on: 08 Dec. 2017.

Asynchronous programming with Async and Await (C # and Visual Basic) . Available at: link . Accessed on: 08 Dec. 2017.

    
08.12.2017 / 13:00