Will a Thread.Sleep () loop be bad for performance when used to pause a thread?

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:

 //Class level. long bytesWritten = 0; bool pause = false; //Method (thread) level. while(bytesWritten < [target file size]) { ...write 4096 byte buffer to file... while(pause == true) { Thread.Sleep(250); } ...do some more stuff... } 

I heard about ResetEvents, and I know a little about what they do, but I never looked at them.

+5
source share
4 answers

I think based on the description I would do it

 'Class level. Private BytesWritten As Long = 0 Private NotPaused As New Threading.ManualResetEvent(True) 

Changing the name of a variable is appropriate, as it will be used in this way

  'Method (thread) level. While BytesWritten < [target file size] '...write 4096 byte buffer to file... NotPaused.WaitOne(-1) '...do some more stuff... End While 

To pause the loop do this

  NotPaused.Reset() 

and continue

  NotPaused.Set() 
+3
source

In .NET, there is no reason to use Thread.Sleep other than trying to simulate lengthy operations when testing and / or debugging in an MTA stream, since it will be blocked.

Perhaps another option would be to use TPL . Since you do not want to block, you can use Task.Delay . As you probably know, a task is an asynchronous operation.

+2
source

I think the more elegant way is to thread thread endlessly until it wakes up with another thread calling Thread.Interrupt on the first thread that slept. Here is a good example of this with code example: Pausing and resuming threads .

+1
source

Perhaps I donโ€™t understand what you are trying to achieve here, but from what I see, it seems that you are trying to block the thread until the I / O task is completed. Semaphores would be best here, instead of executing Thread.Sleep ().

Most operating systems provide blocking semaphores that put the thread to sleep until another thread wakes it up. You are probably better off using them instead of constantly doing it yourself.

Both Thread.Sleep () and blocking semaphores put the thread into sleep mode, but the latter does this constantly until the resource has been freed (the semaphore is signed). The first requires that the thread constantly wakes up, checks and returns to sleep. The latter saves these run cycles.

+1
source

All Articles