There is (or was) a lot of talk about how good or bad it is to use the Thread.Sleep() method. From what I understand, this is mainly used for debugging purposes.
Now I ask myself: is it bad to use for my specific purpose, i.e. constantly loop it in order to be able to pause / resume a thread? I do this because I want to pause a thread that is doing I / O and can resume it in a simple way.
I / O operations basically just write up to 4096 bytes to a file until all the data is written to it. Since the file can be large and for a long time I want it to pause the operation (in case it starts to consume a lot of system resources).
My code, VB.NET version:
'Class level. Private BytesWritten As Long = 0 Private Pause As Boolean = False 'Method (thread) level. While BytesWritten < [target file size] ...write 4096 byte buffer to file... While Pause = True Thread.Sleep(250) End While ...do some more stuff... End While
C # equivalent:
I heard about ResetEvents, and I know a little about what they do, but I never looked at them.
source share