opc0de
opc0de

Reputation: 11768

Slow down a Windows Process?

How can I slow down a Windows Process?

I understand that I need to hook QueryPerformanceCounter but what do I need to do next?

Need help for Delphi or C++

Upvotes: 0

Views: 2655

Answers (4)

Stijn Sanders
Stijn Sanders

Reputation: 36840

Windows 2000 introduced a new layer over internal process management that allows to set extra limits on one or more processes: Jobs. I'm not sure if it allows to set a limit on processor time used, but if the application isn't using the method already, you might just be able to call AssignProcessToJobObject and SetInformationJobObject to impose extra limitations.

Upvotes: 0

Vantomex
Vantomex

Reputation: 2295

I don't understand what is the motive/background of your question since you didn't explain it clearly. However, using SetPriorityClass(), you can sets the priority class for the specified process to BELOW_NORMAL_PRIORITY_CLASS or even to IDLE_PRIORITY_CLASS so that the process running slower; and you can also sets the priority class for the specified process to ABOVE_NORMAL_PRIORITY_CLASS or even to HIGH_PRIORITY_CLASS so that the process running faster. Before doing that, you'll need to get handle of target process by its PID, look here.

Upvotes: 0

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74360

I am not sure I understand the relationship of hooking QueryPerformanceCounter to slowing down a process that you described. Perhaps if you elaborate in the original question or a comment, I can help you further.

To answer your question, you can use cpustres.exe from the Windows 2000 resource kit to put a load on your system, causing context switching. If you put enough of a load and oversubscribe all available CPUs, you will slow down your process. Depending on the load level you select with the cpustres settings, you can slow your process down a lot or a little.

If you want to slow down the process programmatically in a more controlled way without crashing it if it is a game, you can use casablanca's answer, but replace Sleep(10) with:

// Turn off optimizations to make sure the busy wait loops do
// not get optimized out!

HANDLE hThread = ...; // thread that you want to slow down 
for (;;) { 
  SuspendThread(hThread); // Do this for each process thread

  // Busy wait for pause (possibly small)
  const DWORDLONG pauseFactor=1000; // In cycles
  DWORDLONG start=__rdtsc();

  while (__rdtsc()-start<pauseFactor)
    ;  

  ResumeThread(hThread); // Do this for each process thread

  // Busy wait for resume
  const DWORDLONG runFactor=1000; // In cycles
  DWORDLONG start=__rdtsc();

  while (__rdtsc()-start<runFactor)
    ;  
} 

Upvotes: 3

casablanca
casablanca

Reputation: 70701

One way I can think of is to have a dedicated thread use SuspendThread on the thread that you want to slow down, wait for a little while and then resume the thread:

HANDLE hThread = ...; // thread that you want to slow down
for (;;) {
  SuspendThread(hThread);
  Sleep(10); // some number of milliseconds - larger values will slow down more
  ResumeThread(hThread);
}

Upvotes: 1

Related Questions