1

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;
4
  • ThreadPriority define Priority of Threads within a Process. Have you try to up your Process priority?
    – vasily.sib
    Commented Jun 21, 2018 at 8:47
  • Have edited my question to show some of the code tried, but to no effect.
    – Thomas
    Commented Jun 21, 2018 at 10:44
  • Can you extract your CSV parsing code into small app that can still reproduce the issue and be shared? I would be interested in poking with profiler. Maybe you are using thread synchronization between background thread and your UI thread (for example frequently update progress or add results to UI) and most time is spent on synchronization. I haven't seen such dramatic slowdowns if you are not interacting with UI. My "go to" solution is to see performance profile with PerfView tool
    – dlxeon
    Commented Jun 25, 2018 at 8:15
  • Does this answer your question? Will a task be completed faster if it is the active window?
    – starball
    Commented Sep 10, 2023 at 22:34

1 Answer 1

1

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.

1
  • By giving your process one processor core for itself i mean forbiding every other process to use this very core. If no other process is allowed to use a specific core then your process should be able to use it all the time. If you solve this programmatically your software might need administrator privileges.
    – Detonar
    Commented Jun 21, 2018 at 11:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.