Speed: GetAsyncKeyState() slow down dramtically if Taskmgr active

Viewed 51

I use heavily GetAsyncKeyState() to test some visual and speed aspects in the code. What I see by hasard, GetAsyncKesState slow downs by factor about 100 if Taskmgr.exe is active and in foreground.

To see this effect I have written a Hello world programm which check if a key is pressed in an endless loop.

#include <iostream>
#include <windows.h>
#include <chrono>

using std::chrono::high_resolution_clock;
using std::chrono::duration;


#define IS_PRESSED( vk ) ( GetAsyncKeyState( vk ) & 0x8000 )    // Observation: Slow Down dramatically if Taskmgr.exe is active 
#define IS_ACTIVE( vk ) ((GetKeyState(vk) & 0x0001) != 0)


int main()
{
   std::cout << "Hello World!\n";

   auto t1 = high_resolution_clock::now();

   int i = 0;
   while (!IS_PRESSED(VK_ESCAPE))       // Observation:  slow down ~100x if Taskmgr is active
// while (!IS_ACTIVE(VK_ESCAPE))        // ok, no time side effect
  {
     i++;
     if (i % 100000 == 0)
     {
        // Print speed
        auto t2 = high_resolution_clock::now();
        duration<double, std::milli> ms_ElaspedTime = t2 - t1;  /* Getting number of milliseconds as a double. */
        std::cout << "Speed: " << ms_ElaspedTime.count() << " ms / 100'000 loops" << std::endl;
        t1 = high_resolution_clock::now();
     }
   }
}

The Result is:

Speed: 0.445 ms / 100'000 Loops
Speed: 140.577 ms / 100'000 Loops !! (TaskMgr.exe Active)

Does someone have an explanation for this issue?

0 Answers
Related