1

My hosting provider gave me 50% cpu limitation. I'm trying to use DotNetZip to backup my DNN portal files -- the collection of more than 16000 files of 600Mb disk-space. I'm using a separate thread with the lowest priority for compression. If the processor is loaded enough then my thread works fine but when the processor is more or less free I rich quickly my CPU limitation (50%) and finally the pool terminates and needs to be recycled.

So I need an idea how I can slow down the thread in order not to exceed the cpu limitation.

Thanks.

2
  • Would a Thread.Sleep(500) between each file work?
    – agent-j
    Jun 9, 2011 at 18:31
  • Sure it will, but it could be too slow though a few hours -- much easier to backup through ftp.
    – mishau
    Jun 9, 2011 at 20:39

3 Answers 3

1

If you just want to keep the thread below 50% utilization, sprinkle Thread.Sleep(x) throughout the code. You'll need to figure out how many of these you need, and what the millisecond delay should be -- and that's only if you wrote the code that needs the Sleep() calls...

That said, your situation sounds rather odd. There should be a better way to make your backup.

2
  • So Sleep's the only way. I'm thinking of a formula that will calculate the delay time depending on the speed of bytes handled within 1Gb of the total space. With every portion of bytes sent it will calculate the speed and if the speed gets higher than a certain limit it will be corrected by the delay.
    – mishau
    Jun 9, 2011 at 20:36
  • @mishau: You may want to use the Performance Counters, if the security on the box allows you to do so. Jun 9, 2011 at 21:17
0

The simplest approach would probably be to throw in a Sleep regularly, with enough time specified so that thread cannot, even if nothing else is ready to run, it is using less that half of one core.

0
workerThread.Suspend ()
Thread.Sleep (500);
workerThread.Resume ();

Watch out, though. Caution, there is potential to cause deadlocks or unintended slowdowns if the worker thread is in a critical section while you suspend it. For more information: MSDN System.Threading.Thread.Suspend

1
  • Suspend/Resume are obsolete since .net 2.0; And Thread.Sleep() freezes the thread in which it is called (according to my tests).
    – mishau
    Jun 11, 2011 at 18:21

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.