Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » While loop, sleep
While loop, sleep [message #60253] Thu, 26 October 2023 06:50 Go to next message
sniffgriff is currently offline  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 #60254 is a reply to message #60253] Thu, 26 October 2023 09:03 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3375
Registered: August 2008
Senior Veteran
Hi Sniffgriff

Waiting for a button click is very simple, and you don't have to do any looping.
There is a very clear example in Examples/Button.
The program waits, but without spending CPU, and when you click on a button, a text is changed.
You can replace that action with whatever you want.


Best regards
Iñaki
Re: While loop, sleep [message #60255 is a reply to message #60253] Thu, 26 October 2023 11:44 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1096
Registered: August 2007
Senior Contributor
Hi,
Quote:
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?


If you really need to loop -for a long time- in any function in a U++ gui app, you need to use Ctrl::ProcessEvents(). This function processes UI input/events.

E.g.

while(my_condition) {
 Ctrl::ProcessEvents();
 Sleep(20)
}



Best regards,
Oblivion


[Updated on: Thu, 26 October 2023 11:44]

Report message to a moderator

Re: While loop, sleep [message #60257 is a reply to message #60255] Thu, 26 October 2023 16:14 Go to previous messageGo to next message
sniffgriff is currently offline  sniffgriff
Messages: 10
Registered: May 2023
Location: NY
Promising Member
Hi friends, thank you for replying!

The function im trying to run is to move the mouse to a certain position on the desktop, and I want it to loop in a while:

void Random_Mouse()
{

while (1)
{

Ctrl::ProcessEvents();

SetCursorPos(329,654); // move mouse to this position
Sleep(2200);
SetCursorPos(329,784);
Sleep(2200);

}

}


I put this function in a button:


MyAppWindow() {

...

Button_Random << [=] { Random_Mouse(); };
...

}


Even with ProcessEvents, the program stalls for every sleep. Am I placing the function wrong?

Thank you all for your patience with me
Re: While loop, sleep [message #60258 is a reply to message #60257] Thu, 26 October 2023 17:44 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1096
Registered: August 2007
Senior Contributor
Sleep(2200)


This is way too high for a gui to be responsive. 2200 ms -> 2.2 secs. And you have two of them in the loop. It should be at most Sleep(20).


Other than that, if you provide an example package, I can look into the problem.

Best regards,
Oblivion


Re: While loop, sleep [message #60259 is a reply to message #60258] Thu, 26 October 2023 19:10 Go to previous messageGo to next message
sniffgriff is currently offline  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 #60260 is a reply to message #60259] Thu, 26 October 2023 21:03 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1096
Registered: August 2007
Senior Contributor
Ok, the following code works on win 10:

#include <CtrlLib/CtrlLib.h>
#include <windows.h>

using namespace Upp;

static bool sClose;

struct MyApp : TopWindow {
	Button bt;
	MyApp()
	{
		CenterScreen().SetRect(0, 0, 640, 480);
		Add(bt.SetLabel("Click me!").HCenterPos().VCenterPos());
		WhenClose << [=] { sClose = true; };
		bt << [=] {
			Point p;
			GetCursorPos(p);
			while(p.y-- > 0 && !sClose) {
				ProcessEvents();
				SetCursorPos(p.x, p.y);
                                Title("Cursor pos (y): " << AsString(p.y));
				GuiSleep(20);
			}
		};
	}
};

GUI_APP_MAIN
{
	MyApp().Run();
}




Edit: Example refactored.

Best regards,
Oblivion


[Updated on: Thu, 26 October 2023 21:27]

Report message to a moderator

Re: While loop, sleep [message #60262 is a reply to message #60260] Fri, 27 October 2023 17:34 Go to previous messageGo to next message
sniffgriff is currently offline  sniffgriff
Messages: 10
Registered: May 2023
Location: NY
Promising Member
Hi friend thank you for that example.

I believe it kind of works? No matter how big I make the milliseconds in GuiSleep, the speed remains the same.
Re: While loop, sleep [message #60263 is a reply to message #60262] Fri, 27 October 2023 17:43 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 698
Registered: December 2005
Location: Budapest, Hungary
Contributor
Why don't you use Ctrl::SetTimeCallback()?
Re: While loop, sleep [message #60264 is a reply to message #60262] Fri, 27 October 2023 21:50 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1077
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 #60268 is a reply to message #60264] Sun, 29 October 2023 06:18 Go to previous messageGo to next message
sniffgriff is currently offline  sniffgriff
Messages: 10
Registered: May 2023
Location: NY
Promising Member
Thank you friend, I appreciate it.

I see that the random rectangle color is run on a single frame. How would I utilize SetTimeCallback if the loop function I am running requires waiting, between steps? For example, step:1 change the rectangle color to blue, step:2 wait 2 seconds, step:3 then change the color to red, wait 4 seconds?
Re: While loop, sleep [message #60271 is a reply to message #60268] Sun, 29 October 2023 20:22 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1077
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.
Re: While loop, sleep [message #60280 is a reply to message #60271] Tue, 07 November 2023 03:54 Go to previous message
sniffgriff is currently offline  sniffgriff
Messages: 10
Registered: May 2023
Location: NY
Promising Member
Thank you my friend, im going to try this and get back to you! Thank you all very much.
Previous Topic: Java Script
Next Topic: How do I write string_view to Cout() without copying to a String?
Goto Forum:
  


Current Time: Tue Jun 25 03:07:03 CEST 2024

Total time taken to generate the page: 0.01496 seconds