Home » Community » Newbie corner » While loop, sleep
While loop, sleep [message #60253] |
Thu, 26 October 2023 06:50  |
sniffgriff
Messages: 10 Registered: May 2023 Location: NY
|
Promising Member |
|
|
Hello!
I am trying to run a while loop function in U++, and I want it to run indefinitely. I trigger the while loop in a button. However, when I use sleep(), the whole program waits/freezes for the duration of sleep. Is there a way to run the loop parallel? Where do I trigger the loop function? Sorry if this is obvious, but I appreciate the input/advice.
|
|
|
|
|
|
|
Re: While loop, sleep [message #60259 is a reply to message #60258] |
Thu, 26 October 2023 19:10   |
sniffgriff
Messages: 10 Registered: May 2023 Location: NY
|
Promising Member |
|
|
Even if I reduce the sleep to (20), the program as a whole is suspended, even though the loop is running from triggering a button. I guess I wanted the loop to be running at the same time as everything else.
I have the U++ tracking the users mouse position on the desktop, as well as the pixel color of the mouse position. I can manually move the mouse using Windows headers, basically an auto clicker Im trying to create. Move the mouse to a position, wait a moment, click, wait a moment, move to another position, etc.
Will the package include windows header files?
|
|
|
|
|
|
Re: While loop, sleep [message #60264 is a reply to message #60262] |
Fri, 27 October 2023 21:50   |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior Contributor |
|
|
Hello,
I agree with zsolt. SetTimeCallback is the way to go in your case. Currently, I am working on expanding our GUI tutorial about exemplary usage of that feature.
Here is the code:
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
struct MyAppWindow : TopWindow {
Rect rect;
Color color;
MyAppWindow() {
Zoomable().SetRect(0, 0, 200, 200);
RandomizeRect();
SetTimeCallback(-2000, [=] { OnTimer(); });
}
~MyAppWindow() {
KillTimeCallback();
}
void Paint(Draw& w) override {
Size sz = GetSize();
w.DrawRect(sz, White());
w.DrawRect(rect, color);
}
void OnTimer() {
RandomizeRect();
Refresh();
}
void RandomizeRect() {
Size sz = GetSize();
int length = 50;
int x = Random() % (sz.cx - length);
int y = Random() % (sz.cy - length);
rect = Rect(x, y, x + length, y + length);
color = Color(Random() % 255, Random() % 255, Random() % 255);
}
};
GUI_APP_MAIN
{
MyAppWindow().Run();
}
The first parameter in this case mean interval in milliseconds. If it is less than zero it means that it will be repeated infinite number of times. If the number is positive it will be executed only once.
SetTimeCallback(-2000, [=] { OnTimer(); });
In above example, the method OnTimer() in Ctrl will be executed once per 2 seconds until cancellation. Cancellation can be done by calling KillTimeCallback().
Moreover with SetTimeCallback you shouldn't observe any UI freezes or something like that.
Regards,
Klugier
U++ - one framework to rule them all.
[Updated on: Fri, 27 October 2023 21:58] Report message to a moderator
|
|
|
|
Re: While loop, sleep [message #60271 is a reply to message #60268] |
Sun, 29 October 2023 20:22   |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior Contributor |
|
|
Hello sniffgriff ,
You can have more than one TimeCallback or you can spawn one time callback from another. For example
Spawn callbacks:
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
class MyApp : public TopWindow {
MyApp() {
SetTimeCallback(1000, [=] { Action1(); }); // The first parameter is positive, time callback will be executed only once
}
void Action1() {
SetTimeCallback(2000, [=] { Action2(); }); // Execute Action2 after 2 seconds...
}
void Action2() {
SetTimeCallback(5000, [=] { Action3(); }); // Execute Action3 after 5 seconds...
}
void Action3() {
// It's over no new time callbacks will be executed
}
};
Having more than one callback time callback:
class MyApp : public TopWindow {
MyApp() {
// The third parameter of SetTimeCallback is id. If id's are different there are
// separate entities and you can schedule multiple time callbacks at once.
SetTimeCallback(1000, [=] { Action1(); }, 0); // Execute Action1() after 1s
SetTimeCallback(4000, [=] { Action2(); }, 1); // Execute Action2() after 4s
SetTimeCallback(7000, [=] { Action3(); }, 2); // Execute Action3() after 7s
}
void Action1() {}
void Action2() {}
void Action3() {}
};
I hope it will help.
Klugier
U++ - one framework to rule them all.
|
|
|
|
Goto Forum:
Current Time: Fri Apr 25 20:31:53 CEST 2025
Total time taken to generate the page: 0.00981 seconds
|