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 » U++ Library support » U++ MT-multithreading and servers » Dealing with background tasks elegantly in a userinterface
Dealing with background tasks elegantly in a userinterface [message #58159] Fri, 04 March 2022 16:35 Go to next message
Alboni is currently offline  Alboni
Messages: 214
Registered: January 2012
Location: Deventer, Netherlands
Experienced Member
Hello,

I wonder if there is an elegant way to execute tasks in the background. There are often MySql queries or http or rpc calls or other tasks that can potentially take more time than one can spend in a callback if you want a responsive userinterface.
How does one go about that in u++?

Some points:
Executing a query in the background and get a notification to the ui when it is done or failed and how much more time in between. Tasks over 5 seconds should be cancellable.
Do I have to open a MySqlSession for every query that is executed in the background?? Is this an "expensive" or slow thing to do or doesn't it matter.
Can I copy SqlSessions and use the copy in another thread concurrently with the original? DO I have to open it with credentials every time?
Cross thread communication with queues or pipes?
Using callbacks from threads?

About serializing tasks from other threads, is there an example for that?
I don't want to reinvent the wheel and get unreliable code. Done that too many times already.
I want the quick, easy and robust way. I'm missing the howto here, the overview. A reference is not enough.

(Yes, I've found Upp::Thread)


[Updated on: Fri, 04 March 2022 16:36]

Report message to a moderator

Re: Dealing with background tasks elegantly in a userinterface [message #58162 is a reply to message #58159] Sun, 06 March 2022 01:41 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
I use Thread class in my older code with Mutex protected containers as job queue.
Currently I prefer CoWork.
To notify GUI thread, I use PostCallback. It is a very easy way.
GUI is thread safe now, but I haven't tested GUI that way yet.
For HTTP, SMTP I created simple curl wrapper classes with progress support, but now you can use HttpRequest's WhenDo to notify user with a progress bar.
SQL: I use separate, per thread sessions, opened by the thread.
Serialization: Mutex and Mutex::Lock utility class are very useful.
Re: Dealing with background tasks elegantly in a userinterface [message #58163 is a reply to message #58159] Sun, 06 March 2022 09:36 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Hello Alboni

Quote:

I wonder if there is an elegant way to execute tasks in the background. There are often MySql queries or http or rpc calls or other tasks that can potentially take more time than one can spend in a callback if you want a responsive userinterface.
How does one go about that in u++?


Try AsyncWork. It is simple, allows you to cancel tasks, and allows you to elegantly return values. (Based on CoWork). https://www.ultimatepp.org/src$Core$AsyncWork_en-us.html

Tutorial: https://www.ultimatepp.org/srcdoc$Core$Tutorial$en-us.html#S ection_7_5


Best regards,
Oblivion


[Updated on: Sun, 06 March 2022 10:22]

Report message to a moderator

Re: Dealing with background tasks elegantly in a userinterface [message #58165 is a reply to message #58163] Sun, 06 March 2022 18:11 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
After seeing the tutorial, this AsyncWork seems to be a very useful thing for me.
I haven't tried it yet, but almost the same as async/await in Javascript.
Thanks!
Re: Dealing with background tasks elegantly in a userinterface [message #58194 is a reply to message #58162] Fri, 18 March 2022 18:42 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
zsolt wrote on Sun, 06 March 2022 01:41

To notify GUI thread, I use PostCallback. It is a very easy way.


You might enjoy checking out Ctrl::Call. Combined with lambda that is even easier.

Current rules for parallel GUI programming:

- only the main thread can manipulate windows (open, close, move) or run event loop (this is actually a limitation forced by win32 architecture)
- exception to this rule is Prompt* function which open window but can be called from non-main threads
- except this, non-main threads can call widget methods as long as they use GuiLock

Mirek
Previous Topic: httprequest fails getting entire url content
Next Topic: Problem breaking loop (with close button) in main thread
Goto Forum:
  


Current Time: Thu Mar 28 18:21:25 CET 2024

Total time taken to generate the page: 0.00976 seconds