0

The memory consumption in methods Task.Delay and Thread.Sleep is different, I used both and the answer have a big performance difference. Does Anyone know why?

The code with Thread

private void EncerrarWorkers()
    {
        foreach (BackgroundWorker worker in Workers) 
        {
            worker.CancelAsync();
        }

        //Encerra os bots
        Model.BotsClose();

        for (int indexRobo = 0; indexRobo < Workers.Count(); indexRobo++)
        {
            if (Model.IsBotClosed(indexRobo))
                continue;

            Thread.Sleep(5000);
            indexRobo--;
        }
    }

The code With Task

private void EncerrarWorkers()
    {
        foreach (BackgroundWorker worker in Workers) 
        {
            worker.CancelAsync();
        }

        //Encerra os bots
        Model.BotsClose();

        for (int indexRobo = 0; indexRobo < Workers.Count(); indexRobo++)
        {
            if (Model.IsBotClosed(indexRobo))
                continue;

            Task.Delay(5000);
            indexRobo--;
        }
    }

A both are contained in a method of Windows Form.

The performance difference in utilize the block of code: enter image description here

  • 4
    Did you mean to use `Task.Delay` without `await`? Because it probably doesn't do what you want it to do. – John Wu Nov 10 '21 at 03:26

1 Answers1

0

They do quite different things.

Thread.Sleep causes the currently executing thread to just stop running for a given amount of time. It probably doesn't consume additional memory or do much processing, but the entire time it is sleeping it uses a system Thread, which is a limited resource. You could starve your application of threads, or cause another Thread to be created elsewhere, which has big performance impact. But, works great if that one thread is the only thing running on your computer.

Task.Delay creates a Task that will be dormant for the given amount of time and then finish. If you use in an asynchronous method, this allows you to return the thread to the caller, which can use it for something else until the timer is up. This is much more efficient when you have multiple threads, such as in a web server environment or doing a lot of database reads. However, you must use await, otherwise the task is created but then ignored, so it doesn't delay anything. And to use await, you must be in an asynchronous method in an asynchronous stack. You could call Wait() on the task, but that still locks the thread so might as well use Sleep at that point.

It looks like you have some worker threads of some kind. Better to make those tasks and they use .WaitAll on WhenAll or WhenAny to wait until one is actually finished.

Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23
  • 'the entire time it is sleeping it uses a system Thread, which have a lot of overhead'...what overhead? Overhead of what? – Martin James Nov 10 '21 at 16:51
  • @MartinJames. Rephrased. They do typically have a minimum of 1MB stack allocation as well as additional memory for state saving, but that is already allocated once the thread is running. It's the possible creation of a new thread as a result that is potential performance issue – Garr Godfrey Nov 10 '21 at 17:58