One reason is that Thread.Sleep()
blocks your code from other actions. The last effort is to make the lock as small as possible. For example, node.js is a non-blocking language.
Update: I do not know about the infrastructure of the Timer
class in C #. Perhaps this also blocks.
You can schedule a task to check this third API every 100 ms. Thus, within 100 ms your program can perform other tasks.
Update: This analogy may help. If we compare the operating system with the hospital and compare the threads with the nurses in this hospital, the supervisor (programmer) can choose the policy:
- Or ask each nurse (thread) to observe her, and only one patient (work, task that must be completed), even if she waits an hour between each check (
Sleep()
method) - Ask each nurse to check each patient and continue and check other patients during the interval until the next check.
The first model blocks. It does not scale. But in the second model, even with several nurses, you could serve many patients.
source share