Reputation: 548
Edited out : Originally stated two questions, tested one myself and got the answer.
I am creating a threaded reader/writer that looks over files in a folder set. It does this every 5 minutes, or less if a user specifies it.
My current issue is that the thread will look at the files with a tad too much voraciousness. I want to limit the thread to using at the most 30% of the CPU (or a user specified amount, in case they want it faster/slower). The reason for this is that this is a program that is supposed to be always on, and I do not want it to interfere with them browsing the internet or using other programs at the same time. I personally tend to note that threaded programs using 100% of the CPU screw with other programs even when they are set to a lower priority, and am trying to avoid this.
I was thinking of using PerformanceCounter to check the CPU usage, hopefully identify the program, and add more sleep cycles in if the performance (of the CPU as a whole, or my program in specific it finds it) starts to peak. I haven't found any documentation on limiting thread CPU usage other then "lol, why bother?", to "not really feasible" with no reasons why (which is perfectly ok - most people want the thread done ASAP and there may be no way to do this inherent to .Net or another C# library).
Does anyone know of a way other then setting it's priority lower, or adding more overhead by using PerformanceCounter/some other CPU monitor? Thanks.
Upvotes: 3
Views: 6049
Reputation: 7411
CPU load itself is not bad, as long as you do not max out. You will be fine just by reducing the priority of the process. If there are other process with higher priority, they will be prioritized.
Process thisProc = Process.GetCurrentProcess();
thisProc.PriorityClass = ProcessPriorityClass.BelowNormal;
Upvotes: 0
Reputation: 36487
Not sure if I understood you the right way. You check the files every 5 minutes and that checking (or their processing) takes too much CPU time off other processes?
If so, how about slowing it down by pausing inbetween processing (e.g. between files) using Thread.Sleep
?
I don't really think you should worry about some max CPU load (plus configuring this could be cumbersome or confusing for the user as for multicore CPUs it's not always clear on what 100% cpu use actually is, is it 100% on one core or 100% in total?).
The time to pause/sleep could depend on the amount of time passed so far and the amount of time left to do the other stuff, e.g. you could reserve 10 ms per file; if processing took 4 ms, you sleep for 6 ms.
If you're continuously checking the files for checking, ensure to use a FileSystemWatcher
instead, which should solve your problems in this case.
Upvotes: 2
Reputation: 1473
I'm unsure about how you are currently doing the folder watching, but I think you should take a look at the FileSystemWatcher class (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx).
You can set it up to watch a certain path for changes, and it's very light on CPU usage. It's also async (so you don't even need a separate thread).
Upvotes: 2