Why do all my programs use 100% CPU?

Nov 22, 2008 at 4:06pm
For example
1
2
3
4
5
6
7
8
9
#include <windows.h>

int main(){
    POINT p;
    do{
        GetCursorPos(&p);
    }while(p.x>10);
    return 0;
}

Help please
Nov 22, 2008 at 5:00pm
Because it is the only program making use of the CPU at the time, so it is given 100% of the resources. Try running two of your programs at once. Or try running your program alongside some other program that is doing a lot of work.
Nov 22, 2008 at 5:28pm
It is because you are constantly polling the cursor position (or whether or not there is keyboard input, etc) instead of letting windows tell you where it is.

I presume you are playing with a console application (and not a GUI application)? To get mouse events in a Win32 console application, assume the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <windows.h>

const char ERR_NOT_A_CONSOLE[] =
  "You must run this program interactively in a console window.\n";

//-----------------------------------------------------------------------------
HANDLE hstdin;
HANDLE hstdout;
BOOL   done = FALSE;

//-----------------------------------------------------------------------------
void write( const char* message )
  {
  DWORD foo;
  WriteConsole(
    hstdout,
    (const void*)message,
    lstrlen( message ),
    &foo,
    NULL
    );
  }

//-----------------------------------------------------------------------------
COORD get_cursor_pos()
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo( hstdout, &csbi );
  return csbi.dwCursorPosition;
  }

//-----------------------------------------------------------------------------
void set_cursor_pos( COORD pos )
  {
  SetConsoleCursorPosition( hstdout, pos );
  }

//-----------------------------------------------------------------------------
void key_event_handler( KEY_EVENT_RECORD event )
  {
  if (event.wVirtualKeyCode == VK_ESCAPE) done = TRUE;
  }

//-----------------------------------------------------------------------------
void mouse_event_handler( MOUSE_EVENT_RECORD event )
  {
  char message[ 100 ];
  COORD pos;
  pos = get_cursor_pos();
  wsprintf(
    message,
    "%3d %3d [%c][%c][%c]",
    event.dwMousePosition.X,
    event.dwMousePosition.Y,
    (event.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED) ? 'x' : ' ',
    (event.dwButtonState & FROM_LEFT_2ND_BUTTON_PRESSED) ? 'x' : ' ',
    (event.dwButtonState & RIGHTMOST_BUTTON_PRESSED) ? 'x' : ' '
    );
  write( message );
  set_cursor_pos( pos );
  }

//-----------------------------------------------------------------------------
int main()
  {
  DWORD        mode;
  DWORD        count;
  INPUT_RECORD console_event;
  BOOL         blink = TRUE;

  // Validate the console
  hstdin  = GetStdHandle( STD_INPUT_HANDLE );
  hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
  if (!GetConsoleMode( hstdin, &mode ))
    {
    WriteFile(
      GetStdHandle( STD_ERROR_HANDLE ),
      (const void*)ERR_NOT_A_CONSOLE,
      lstrlen( ERR_NOT_A_CONSOLE ),
      &count,
      NULL
      );
    return 1;
    }

  // Select raw input and mouse events
  SetConsoleMode( hstdin, ENABLE_MOUSE_INPUT );


  write( "Press Esc to quit.\n." );

  // Main loop
  while (!done)
    {
    if (WaitForSingleObject( hstdin, 350 ) == WAIT_OBJECT_0)
      {
      if (!ReadConsoleInput( hstdin, &console_event, 1, &count )) continue;

      switch (console_event.EventType)
        {
        case KEY_EVENT:
          key_event_handler( console_event.Event.KeyEvent );
          break;

        case MOUSE_EVENT:
          mouse_event_handler( console_event.Event.MouseEvent );
          break;

        }
      }

    blink = !blink;
    if (blink) write( "\b." );
    else       write( "\b " );
    }


  // All done
  SetConsoleMode( hstdin, mode );
  return 0;
  }

If you want the mouse to work, make sure that Properties --> Options --> Quick Edit Mode is OFF. I still don't know how to change that programmatically.

Here's a tutorial I found that you might like.
http://www.adrianxw.dk/SoftwareSite/index.html
Nov 22, 2008 at 5:29pm
The reason regular programs don't use 100% CPU is because they spend most of the time either waiting for input (for example, a text editor), or inside a delay function (such as Sleep()).
The problem in your case is that you're constantly checking where the mouse is, while the user is not constantly moving the mouse around. I reckon there must be some function that waits for the user to perform some input. if there isn't, then at least you can wait a certain amount of time between each check. Even waiting 10 ms will lower CPU usage to about 0% (depending on the system).
Topic archived. No new replies allowed.