Home » U++ Library support » U++ MT-multithreading and servers » Problem breaking loop (with close button) in main thread
Re: Problem breaking loop (with close button) in main thread [message #59048 is a reply to message #59044] |
Thu, 20 October 2022 20:10   |
Oblivion
Messages: 1206 Registered: August 2007
|
Senior Contributor |
|
|
Hi Lance,
Quote:
probably should not modify GUI from within other than the GUI thread.
You are allowed to modify GUI from within other threads. For this purpose, you need to use
a) EnterGuiMutex() & LeaveGuiMutex();
b) GuiLock, which is basicly the above functions wrapped in a class for convenience:
Rudimentary example:
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
#define LAYOUTFILE <DeadMutex/DeadMutex.lay>
#include <CtrlCore/lay.h>
static std::atomic<bool> fin;
class DeadMutex : public WithDeadMutexLayout<TopWindow> {
typedef DeadMutex CLASSNAME;
public:
DeadMutex()
{
CtrlLayout(*this, "Window title");
start.WhenPush = THISFN(Start);
stop.WhenPush = [this]{ fin = true; };
WhenClose = [this]{ ShutdownThreads(); Break(); };
}
void Start(){
Thread().Run([=] {
{
GuiLock __; // EnterGuiMutex()
log.Append("Badguy instance running & owned the gui mutex...\n");
// LeaveGuiMutex();
}
while(!fin && !IsShutdownThreads() ){
Sleep(50);
}
GuiLock __;
log.Append("Stop requested, Badguy instance exiting...\n");
fin = false;
});
}
};
GUI_APP_MAIN
{
DeadMutex().Run();
}
There is also a GuiUnlock class which basically reverses the order of LeaveGuiMutex and EnterGuiMutex, so you can force the main thread to temporarily unlock its GUI lock.
What you are not allowed to do is, creating windows and prompts within other threads.
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Thu, 20 October 2022 20:12] Report message to a moderator
|
|
|
 |
|
Problem breaking loop (with close button) in main thread
By: awksed on Tue, 18 October 2022 17:19
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Tue, 18 October 2022 19:15
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Tue, 18 October 2022 20:09
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: awksed on Wed, 19 October 2022 01:11
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Wed, 19 October 2022 02:21
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Wed, 19 October 2022 02:42
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: awksed on Wed, 19 October 2022 20:56
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Thu, 20 October 2022 01:04
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Oblivion on Thu, 20 October 2022 20:10
|
 |
|
Re: Problem breaking loop (with close button) in main thread
By: Lance on Thu, 20 October 2022 21:49
|
Goto Forum:
Current Time: Sun May 11 17:39:19 CEST 2025
Total time taken to generate the page: 0.02482 seconds
|