I often see it mentioned that Thread.Sleep();
should not be used, but I can\'t understand why this is so. If Thread.Sleep();
can cause trouble, are
I have a use case that I don't quite see covered here, and will argue that this is a valid reason to use Thread.Sleep():
In a console application running cleanup jobs, I need to make a large amount of fairly expensive database calls, to a DB shared by thousands of concurrent users. In order to not hammer the DB and exclude others for hours, I'll need a pause between calls, in the order of 100 ms. This is not related to timing, just to yielding access to the DB for other threads.
Spending 2000-8000 cycles on context switching between calls that may take 500 ms to execute is benign, as does having 1 MB of stack for the thread, which runs as a single instance on a server.