Problem with Endless loop

So i wrote this code...

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
double spin(int);
void   RadiusChange();
bool Control = false;
int  radius;;

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int showcmd)
{

	char Buff[1];

	ifstream f;
	f.open("Settings.txt");
	f.getline(Buff,3);
	f.close();

	radius = atoi(Buff);

	Sleep(2000);

  POINT pt;
  GetCursorPos(&pt);

  int degrees = NULL;
  

	while (1)
	{
	
    Sleep(20);

    int x = int(pt.x + radius*cos(spin(degrees)));
    int y = int(pt.y + radius*sin(spin(degrees)));

    SetCursorPos(x, y);
    degrees = (degrees + 1) % 360;

	RadiusChange();
	
	}


double spin(int degrees)
{
  const double pi = 3.1415926;
  return degrees * pi / 180.0 + 0.5;
}

void RadiusChange()
{

	if(Control == false)
		{
			radius--;
			
			if(radius < 2)
				Control = true;
		}
	else
		{
			radius++;
			
			if(radius > 255)
				Control = false;
		}
}




It's an endless loop that moves the mouse in a circular pattern...problem is since its an endless loop i can't put this code with a GUI without getting it stuck...it won't process incoming messages or anything...i have to use TaskMgr to shut it down...any idea how to solve it?

thx
Last edited on
Don't use while(1)? Use a non-blocking poll for input or something.
I have no idea what that means...maybe an example or something.
In order to have a GUI, you need to have a messge pump. A message pump is a loop where you process Windows notifications and messages. If I remember correctly, you use GetMessage() to retrieve a message from the queue, then use TranslateMessage() to translate any accelerators, and then use use DispatchMessage() to have the message sent to the Window Procedure of the corresponding window.

You can find the example from Microsoft here: http://msdn.microsoft.com/en-us/library/ms644928%28VS.85%29.aspx.

Now, note that GetMessage() is a blocking call. This means that it will block execution of the thread if there are no incoming messages or notifications. So, what you need is:

A. Change the loop and use PeekMessage(), which is non-blocking. If there are no messages, then you take the opportunity to programatically move the mouse. If there are messages, then you translate and dispatch them.
B. Make your application spawn a worker thread that moves the mouse in the circular fashion.
Topic archived. No new replies allowed.