Thread: Simple Win32 console app using 99% of CPU?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Simple Win32 console app using 99% of CPU?

    Here is my basic code...

    Code:
    #include <iostream> 
    #include <windows.h>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow(); // this needs to be declared because microsoft forgot it :P
    
    int main() {
    	/* Hide window */
    	HWND hWnd;
    	AllocConsole(); 
    	hWnd = GetConsoleWindow();
    	ShowWindow(hWnd, SW_HIDE);
    	/***************/
    
    
    	while(1) {
               //Some stuff here
    
    	}
    
    	return 0;
    }
    Within thw while loop there is some simple code that shouldn't take much processor time. But on the WinXP task manager, it shows it using 99% of CPU. Any way to make it faster? I don't need it to update so often, only like once every second.. would a Sleep(1000); at the end of the while loop speed it up? thanks!

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Use this as your loop instead:

    Code:
    	MSG msg;
        while(GetMessage(&msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    That didn't help it at all :S

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You really mis-understand this speed issue don't you.

    Compare
    Code:
        int i = 0;
        while(1) {
            cout << i << endl;
            i++;
        }
    With
    Code:
        int i = 0;
        while(1) {
            cout << i << endl;
            i++;
            Sleep(1000);
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    No, I understand.

    "I don't need it to update so often, only like once every second.." ~me

    I don't want my program to run faster, I want it to use the LEAST amout of proccesser time and resources. How might that be done? How come other proccesses can run using 0% or 1% of CPU power but mine takes 99%?

    Any ideas?
    Last edited by c_olin3404; 02-17-2005 at 02:40 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because they either
    - Sleep for some period of time before doing something
    - Wait for a message to arrive (say a key press or a mouse movement)
    - Wait for some other event (say network traffic)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 console, xp,nt,2000 only?
    By Ash1981 in forum C Programming
    Replies: 8
    Last Post: 01-01-2006, 03:09 AM
  2. Need help migrating console app to windows app
    By DelphiGuy in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 07:05 PM
  3. Win32 Console App... multiple console buffers
    By Trauts in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2003, 11:26 AM
  4. Simple GUI for console app (How?)
    By Bill 101 in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 03:16 PM
  5. Win32 Console Application...Full Screen?
    By MrWizard in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2002, 02:56 PM