Piotr Krzemiński
Piotr Krzemiński

Reputation: 23

Force foreground processing in WindowsAPI

I have an executable program that performs latency measurements. C++ pseudo-code below:

void main(){
  lock_priority();

  start_measurements();
  work();
  end_measurements();
}

The work() creates multiple threads and takes a long time to complete, so ideally I'd like to minimize the executable console when the process is running, just to save screen space. This, however, reduces the output latency by around 50% compared to when not minimized.

I'd like to implement the lock_priority() function so that even when minimized, the process does not go into PROCESS_MODE_BACKGROUND_BEGIN mode.

What I've tried so far

  1. SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); - did not work
  2. Created a thread that every few seconds calls the function above - it did work, but, scientifically speaking, "it looks ugly"
  3. I have tried to find a method to attach a callback to the SetPriorityClass() function so that after it finishes if the PriorityClass was anything but REALTIME_PRIORITY_CLASS, it'd re-set it again (or at least set PROCESS_MODE_BACKGROUND_END priority). This sounds like a perfect solution, but I could not find anything in the docs about this.
  4. I discovered there is a way to set the processor to prefer foreground/background tasks (reference) - however even if this was possible to be configured through code, I still need a way to bind this theoretical function to the priority change.

Any help would be very appreciated!

Upvotes: 0

Views: 130

Answers (1)

nick
nick

Reputation: 614

How about redirecting the programm output from console to a file or just buffer it, like here:

Redirect both cout and stdout to a string in C++ for Unit Testing

This way, you don't have any console latency at all - if this is alright for your testing.

Upvotes: 2

Related Questions