I don't see what's wrong with just looping some maximum number of times and then stopping. It limits the actual amount of computation to a fixed amount. This will correspond to a different amount of time on different systems, but does it matter?
If you need to abandon processing until some event occurs, I do not see how you can avoid a call to WaitForSingleObject() or similar. It entails overhead, but what other choice do you have?
Thanks for the comments!
@iMalc "The fact that your real code will actually be doing something useful will mean that overall it wont be spending quite as much time sleeping."
Yes i didnt realize that, my bad.
int iX = 0;
int iY = 0;
int iScreenX = GetSystemMetrics(SM_CXSCREEN);
int iScreenY = GetSystemMetrics(SM_CYSCREEN);
DWORD oldtick = GetTickCount();
if (GetWindowRect(hWnd, &rcWndSz))
iX = rcWndSz.left + ((rand() % 5) * ((rand() % 100) > 50 ? 1 : -1));
iY = rcWndSz.top + ((rand() % 5) * ((rand() % 100) > 50 ? 1 : -1));
iX = iX > iScreenX ? iScreenX : iX;
iY = iY > iScreenY ? iScreenY : iY;
iX = iX < 0 ? 0 : iX;
iY = iY < 0 ? 0 : iY;
SetWindowPos(hWnd, NULL, iX, iY, 0, 0, SWP_NOSIZE);
if ((GetTickCount() - oldtick)> 3000) break;