简体   繁体   English

当非活动窗口时应用程序速度降低

[英]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. 我的应用程序正在对后台线程设置为最高线程优先级的CSV文件进行一些简单的读取和处理。 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. 即使打开记事本,阅读过程也会慢大约10倍,而当我单击返回到应用程序时,它的速度会恢复到正常速度。

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. 我以为使用ThreadPriority旨在实现此目的,但似乎没有达到预期的效果。

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. 如果这还不能满足您的要求,您可以尝试为该进程提供一个单独的cpu内核。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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