'Delaying a task for a very very long time
If we delay a Task using the Task.Delay
method for a very long time, let's say 6 months, and if we assume that the OS does not shutdown or restart during this period of time, is there any guarantee the associated Task would wake up at the right time? Or will it be killed by the OS?
Solution 1:[1]
You cannot use Task.Delay
for that length of time. The method takes an int
number of milliseconds, meaning the maximum number you can pass in is int.MaxValue
. That translates to approximately 24.86 days.
The overload that takes a TimeSpan
is also restricted according to the docs:
Exception: The
delay
argument'sTotalMilliseconds
property is greater thanMaxValue
.
If you want a task to happen after a longer period, there are far better tools for that, such as Hangfire or even the operating system task scheduler.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DavidG |