简体   繁体   中英

Application slows down when not the active window

My application is doing some simple reading and processing of CSV files on a background thread, set to highest thread priority. However, I notice that the process is dramatically slower when the application is not the active window. Even opening notepad makes the reading process about 10x slower, while the moment I click back to the application it speeds back up to the normal speed.

Is there any way to prevent the slowdown from happening? I thought using ThreadPriority was intended to achieve this, but it does not seem to have the desired effect.

Have tried the following:

Thread.CurrentThread.Priority = ThreadPriority.Highest;
System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime;
System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)0x0002;

This is probably related to a behavior of the Scheduler of your OS. They tend to increase the priority of processes with active window to increase user experience, therefore reducing priority of all the other processes.

Increasing the thread priority does not work because this behavior is process-related so you could try to raise the process' priority instead.

If this also does not suffice you could try to give this process one cpu core just for itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM