views:

47

answers:

2

I would like to write a .NET application that runs in the background (stays down in the taskbar) and does a long operation like copying folders of files from one location to another. I want to write it in such a way that other applications that are running don't slow down much. I don't care how long the background app takes. Is there a way that I can limit the processing/memory resources for the application. I know when you create threads you can set their priorities to different levels. Would setting a thread priority to low have this effect or would it only have less priority than any other threads that are part of the same application?

+3  A: 

Yes, you can create a new Thread and set the Priority property before starting it:

Thread thread = new Thread(MethodToRun);
thread.Priority = ThreadPriority.Lowest;
thread.Start();
Steven
+1  A: 

If you're looking for something with the precision of NICE in unix, I'm not sure of any exists. But you can set the priority of the process which may help.

System.Diagnostics.Process.GetCurrentProcess().PriorityClass =System.Diagnostics.ProcessPriorityClass.Low;

Hope that helps.

Jeremy Morgan
What will this do differently than Stevens suggestion?
Jordan S
Steven's solution works per thread, my post was per process. If I understand it correctly, by setting the priority class all threads in your process will be prioritized relative to the process itself. By setting the process priority you're capping every thread to that priority. However Steven's solution may be better, because you may want that kind of finite control, rather than dropping the priority of everything. I have +1 his solution as well.
Jeremy Morgan
"Steven's solution may be better". You read that Jordan? Mark me as answer :-P
Steven