Skocz do zawartości

Dlaczego prosty program zajmuje 97% czasu procesora?


Poltergeist

Recommended Posts

Mam taki problem - kod jest banalny - oprocz wyswietlenia okienka nie robi kompletnie nic. A jednak - zajętość procesora nie schodzi poniżej 97%. Dlaczego tak jest i co zrobic, zeby tak nie bylo?

#include <windows.h>

// Global event handle. This event is set when we the main window thread
// wants to finish its existence
HANDLE hGlobalEvent; 



LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
        switch(Msg)
        {
                
                // message send when we want to close the window
                case WM_DESTROY:
                {
                        PostQuitMessage(0);
                        break;
                }
                
                // default window procedure
                default:
                        return DefWindowProc(hWnd, Msg, wParam, lParam);
        }
        
        return 0L;
}




DWORD WINAPI mainWindowThread(LPVOID lpParameter)
{
        HINSTANCE hInstance = *((HINSTANCE*)(lpParameter));
        // define the new window class structure
        WNDCLASSEX wc;
        
        wc.cbSize                       = sizeof(WNDCLASSEX);
    wc.style                    = CS_VREDRAW | CS_HREDRAW;
    wc.lpfnWndProc              = WndProc;
    wc.cbClsExtra               = 0;
    wc.cbWndExtra               = 0;
    wc.hInstance                = hInstance;
    wc.hIcon                    = NULL;
    wc.hCursor                  = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground    = (HBRUSH)GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName             = NULL;
    wc.lpszClassName    = "MAIN_WINDOW";
    wc.hIconSm                  = NULL;
        
        // register new window class
        if(!RegisterClassEx(&wc))
                return 1;

        // create main window program based on the previously registered window class
        HWND hWnd = CreateWindowEx( NULL, "MAIN_WINDOW", "main_window", WS_OVERLAPPEDWINDOW,
                                                                50, 50, 800, 600, NULL, NULL, hInstance, NULL);
        // show the window                                                      
        ShowWindow(hWnd, SW_NORMAL);
        
        
        // message loop
        MSG message;
        while(true)
        {
                if( PeekMessage(&message, NULL, 0, 0, PM_REMOVE) )
                {
                        if(message.message == WM_QUIT)
                                break;
                        
                        TranslateMessage(&message);
                        DispatchMessage(&message);
                }
        }
        
        SetEvent(hGlobalEvent);
        
        return message.wParam;
}





int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        // we have to synchronize the main window thread with the main program thread,
        // meaning that until the main window thread doesn't exit, the main program
        // thread doesn't exit as well...
        
        // create a synchronizing event with initial state set to false
        hGlobalEvent = CreateEvent(NULL, true, false, "MAIN_WINDOW_THREAD_EVENT");
        
        // create a thread for the main window
        DWORD ThreadID;
        HANDLE hThread = CreateThread(NULL, 0, mainWindowThread, &hInstance, 0, &ThreadID);
        
        // wait until the state of the synchronizing event is set to true
        if( WaitForMultipleObjects(1, &hGlobalEvent, true, INFINITE) != WAIT_OBJECT_0 )
                MessageBox(NULL, "Synchronization error", "Error", MB_OK);
        
        // close the main window thread handle
        CloseHandle(hThread);

}

Z gory dzieki za pomoc

Link do komentarza
Udostępnij na innych stronach

  • 3 weeks later...

Ponieważ używasz PeekMessage() co powoduje jedynie "zajrzenie" do kolejki komunikatów windows obadanie czy nic tam nie leży i oddanie sterowania do programu, a że masz to pięknie zrobione w pętli while(true) no to wykonuje to tak szybko jak tylko potrafi, polecam użycia

while(GetMessage(&message, NULL, 0, 0) )
{
	TranslateMessage(&message);
	DispatchMessage(&message);
}

(no chyba że tam jeszcze będziesz chciał coś dodać co spowolni działanie programu)

pozdrawiam

  • Like 1
Link do komentarza
Udostępnij na innych stronach

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...