I would recommend using a ManualResetEvent
(or other WaitHandle
), and calling ManualResetEvent.WaitOne.
This will have a similar effect to sleeping forever, except that it provides you a clean way to exit from your infinite "block" when desired (by calling Set()
on the event).
Using while(true)
will consume CPU cycles, so it's definitely something to avoid.
is there anything that I should not be doing, such as modifying a static collection declared in the scope of the class?
In general, no. Since your thread will be blocked, there shouldn't be any synchronization issues with using shared data (provided the items within the collection don't have specific requirements, such as user interface elements which must be used on a thread with a proper synchronization context.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…