C # processor usage limit

Edited: Two questions were originally posed, they were checked and received an answer.

I am creating a multi-threaded reader / writer that browses files in a set of folders. He does this every 5 minutes or less if the user indicates it.

My current problem is that the stream will look at files with too much gluttony. I want to limit the thread to using a maximum of 30% of the processor (or the user-specified amount if they want faster or slower). The reason for this is that it is a program that should always be turned on, and I do not want it to stop them from browsing the Internet or using other programs at the same time. I personally note that streaming programs that use 100% of the CPU screw with other programs, even if they are set to a lower priority, and try to avoid this.

I thought about using PerformanceCounter to check CPU usage, I hope that it identifies the program and adds more sleep cycles if performance (in general, and in my program, in particular, it finds) starts to peak. I did not find any documentation on limiting the use of a thread processor, except for "lol, why bother?", "Not really feasible" for no reason (which is completely normal - most people want the thread to be executed as soon as possible, and cannot be a way to make it inherent in .Net or another C # library).

Does anyone know another way to set the priority lower or add extra overhead using the PerformanceCounter / some other CPU monitor? Thank.

+3
source share
3

, , , FileSystemWatcher (http://msdn.microsoft.com/en-us/library/system.io. filesystemwatcher.aspx).

, , . async ( ).

+2

, . 5 , ( ) ?

, , (, ), Thread.Sleep?

, ( , , 100% , 100% 100%?).

/ , , , 10 ; 4 , 6 .

, , FileSystemWatcher, .

+2

, . , . , .

Process thisProc = Process.GetCurrentProcess();
  thisProc.PriorityClass = ProcessPriorityClass.BelowNormal;
0

All Articles