4

I wonder whether there are any use cases for Task.Delay(-1) or Task.Delay(TimeSpan.FromMilliseconds(-1)). According to respective documentations, that methods create tasks that wait indefinitely before completing. I know that await Task.Delay(...) does not block a thread nor consumes much resources, but I cannot think of any scenario when wasting some resources is actually useful.

My question is specifically about overloads of Task.Deley() without cancellation token - overloads that include cancellation token might be a bit more useful as the wait period can be interrupted.

Am I missing some use cases for tasks that wait indefinitely? Are they there only for consistency between method overloads?

Tomasz Maczyński
  • 973
  • 10
  • 24
  • Are you thus asking is there a use case for a task that once started waits indefinitely to be triggered instead of only creating a task at the moment you need it? – iheanyi Aug 19 '16 at 22:08
  • @iheanyi not realy. What I mean is that if you create a task, you ultimately want to start it - so if you have a `var t = Task.Delay(-1)`, at some point you want to `await t` (or await combination of `t` and some other tasks) and awaiting `t` does not make any sense for me. – Tomasz Maczyński Aug 19 '16 at 22:15
  • 3
    Have you seen this: http://stackoverflow.com/questions/10741305/why-does-task-delay-allow-an-infinite-delay – iheanyi Aug 19 '16 at 22:30
  • 1
    To be clear, you are talking about `System.Threading.Timeout.Infinite` (https://msdn.microsoft.com/en-us/library/system.threading.timeout.infinite(v=vs.110).aspx) **The value of this field is -1 (0xFFFFFFFF)** – djv Aug 19 '16 at 22:39
  • @Verdolino Yes, that's probably not a coincidence that `System.Threading.Timeout.Infinite = -1`, but documentation suggests using simply `-1` in `Task.Delay` method – Tomasz Maczyński Aug 19 '16 at 22:46
  • @iheanyi I've just found it. I think that it explains only overloads of `Task.Delay` with cancellation token (because test must be cleaned up, app has to be stopped at some point etc). Threads can be woken up from sleep by using `Thread.Interrupt` method so indefinite timeout for threads makes more sense. – Tomasz Maczyński Aug 19 '16 at 22:50
  • 1
    @TomaszMaczyński: Hmm, I can't think of a single one... Indeed, it would seem to violate [Stephen Toub's advice here](https://blogs.msdn.microsoft.com/pfxteam/2011/10/02/dont-forget-to-complete-your-tasks/). – Stephen Cleary Aug 20 '16 at 01:16

1 Answers1

7

I don't think they intended it to be use as Task.Delay(-1), but rather the Task.Delay(-1, cancellationToken). That way you can start your task and block it forever or until the cancellationToken (in this case a "start token") is triggered. I can't think of a reason to do .Delay(-1) on its own as that would also block the process from exiting.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • I had similar feelings, so I included the second paragraph of my question: "My question is specifically about overloads of Task.Delay() without cancellation token - overloads that include cancellation token might be a bit more useful as the wait period can be interrupted." – Tomasz Maczyński Aug 19 '16 at 22:51