5

I have a task that i want to run over and over. I want to call this as fast as possible however i want to keep my cpu cycles low. I have read using Sleep in a backgroundworker is not the best choice. However without sleep my cpu stays about 55% percent. I have an example of my source below. If sleep is appropriate how do you go about choosing the best time to sleep, as you can see from my results below i get different results based on how long i sleep. Can someone let me know what best practice is when doing something similar

BackgroundWorker _Worker = new BackgroundWorker()
{
  WorkerReportsProgress = true,
  WorkerSupportsCancellation = true
};

void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    // do my task here

    // if i uncomment one of the Sleep below I reduce the CPU like so
    // no sleep             CPU = 52-58%
    // Thread.Sleep(1);     CPU = 09-15%
    // Thread.Sleep(10);    CPU = 02-03%
    // Thread.Sleep(100);   CPU = 00-01%
}

void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled) { return; }

    _Worker.RunWorkerAsync();
}
4
  • Based on how you are using it, perhaps a Timer would be more appropriate.
    – Ian Mercer
    Nov 27, 2011 at 3:21
  • I thought about timer but my task takes anywhere from 5 secs to 5 mins to complete so I thought a backgroundworker would be a better choice.
    – poco
    Nov 27, 2011 at 3:26
  • 2
    Why are you concerned about CPU %? Perhaps putting it on a lower priority thread would be more appropriate if you want to ensure that the rest of the system remains responsive.
    – Ian Mercer
    Nov 27, 2011 at 3:59
  • depending on your application it may be an option to use semaphores to signal worker threads when there is work to do, or to block them when not ... Nov 27, 2011 at 5:48

3 Answers 3

1

With multi-threading, it's common to set background work to a lower priority when you don't want to consume the machine. It will use whatever CPU is left over. That means you shouldn't care if it goes to 100%.

But, the problem with BackgroundWorker is you can't set the priority.

Another option is to spawn your own thread which you can set the priority on.

Here's another related SO post:

.NET Backgroundworker Object's Thread Priority

Also, another (likely less attracted) option is to put the heavy work into another process and make that run low priority. If you don't need to communicate alot of progress, it's simple. Otherwise it's more complex to open a communication channel.

How do I launch a process with low priority? C#

1

Deterministic sleeping is not the best optimization you can have in most scenarios (it is just a random number in your case and not a perfect optimization you can have), rather wait and signal is the best choice. You can try lowering thread priority for now but i would highly recommend to get the best performance use wait and signal paradigm

0

Giving your background thread a lower priority will ensure your processing doesn't interfere with other functions (just give it a low enough priority). However, is that a solution for you? You want the background work to run continuously. Perhaps running slowly is not a good option for you?

What is it you're really trying to achieve?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.