game laggs after injecting my code
<p>I made .dll which I am injecting into game. It runs pixel detection after I press alt + s but the game laggs. Is there any possibility to fix it?</p>

<p>It detects red color, presses mouse3 and in-game it shoots but too slow and game is lagging.</p>

<p>I tried to remove Sleep() but it lag more. Any suggestions?</p>

<pre class="lang-cpp prettyprint-override"><code>#include &lt;windows.h&gt;
#include &lt;gdiplus.h&gt;

const int SX = GetSystemMetrics(SM_CXSCREEN);
const int SY = GetSystemMetrics(SM_CYSCREEN);

const int SCREEN_X = (SX/2);
const int SCREEN_Y = (SY/2);
const COLORREF red=RGB(255, 0, 0);
const int Sound[]={SND_ALIAS_SYSTEMASTERISK,SND_ALIAS_SYSTEMEXCLAMATION};
const int State[]={MOUSEEVENTF_MIDDLEDOWN,MOUSEEVENTF_MIDDLEUP};

bool PixelCheck(HDC hdc)
{
    time_t stop=GetTickCount()+50;
    bool result=false;
    while(GetTickCount()&lt;stop) if(GetPixel(hdc,SCREEN_X,SCREEN_Y) == red) result=true;
    Sleep(1);
    return result;
}

DWORD WINAPI ThreadFunction(PVOID pvParam)
{
    HDC hdc=GetDC(0);
    bool shotbot=false,isdown=false;
    INPUT ip;
    ip.type=INPUT_MOUSE;
    ip.mi.dx=0;
    ip.mi.dy=0;
    ip.mi.dwExtraInfo=0;
    while(true)
    {
        if(GetAsyncKeyState(0xA4) &amp;&amp; GetAsyncKeyState(0x53))
        {
            shotbot=!shotbot;
            PlaySound((LPCSTR)Sound[shotbot],NULL,SND_ALIAS_ID);
        }
        Sleep(1);
        if((shotbot&amp;&amp;PixelCheck(hdc))||isdown)
        {
            ip.mi.dwFlags=State[isdown];
            SendInput(1,&amp;ip,sizeof(INPUT));
            isdown=!isdown;
        }
    }
    ReleaseDC(0, hdc);
    return 0;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved) 
{
    if(fdwReason==DLL_PROCESS_ATTACH) SetThreadPriority(CreateThread(0,0,ThreadFunction,0,0,NULL),THREAD_PRIORITY_NORMAL);
    return TRUE;
}
</code></pre>
