|
|
Home » U++ Library support » U++ MT-multithreading and servers » Code before Thread.Run() nor executed
Code before Thread.Run() nor executed [message #48661] |
Mon, 14 August 2017 13:23 |
rafiwui
Messages: 105 Registered: June 2017 Location: Stuttgart, Germany
|
Experienced Member |
|
|
I have these methods:
void StartGeneration()
{
// Disable buttons
buttonBar.generate.Enable(false);
buttonBar.next.Enable(false);
buttonBar.previous.Enable(false);
// Switch page
displayLayouts[buttonBar.pageIndex].Show(false);
generationProgress.Show(true);
// Setup main progress bar
generationProgress.progressMain.Set(0, 3);
// Start thread
generationThread.Run(THISBACK(Generate));
//Thread::Start(THISBACK(Generate));
return;
}
It is called from a button click event.
My problem is that nothing but the Run()-method of the thread is executed or at least there is now visual change in my application but there should be a layout change.
Also it is kinda not started as a thread because the application "freezes" while the thread is running.
Any ideas how I can fix this?
EDIT: Because I was asked for in this thread: Here is an example application to reproduce the problem.
-
Attachment: Bugtester.7z
(Size: 1.00KB, Downloaded 279 times)
Greetings
Daniel
[Updated on: Tue, 15 August 2017 09:06] Report message to a moderator
|
|
|
|
|
Re: Code before Thread.Run() nor executed [message #48671 is a reply to message #48669] |
Tue, 15 August 2017 18:45 |
Oblivion
Messages: 1112 Registered: August 2007
|
Senior Contributor |
|
|
As I suspected, you are calling GUI elements/methods from a worker thread directly. You need to use PostCallback, and make sure the thread terminates.
Here you go:
TestLayout::TestLayout()
{
CtrlLayout(*this);
button1 << THISBACK(OnCLickButton);
}
void TestLayout::OnCLickButton()
{
WhenThreadStart();
}
TestLayout2::TestLayout2()
{
CtrlLayout(*this);
progress.Set(0, 1);
}
TestWindow::TestWindow()
{
SetRect(0, 0, Zx(400), Zy(200));
Add(testLayout);
Add(testLayout2);
testLayout.Show(true);
testLayout2.Show(false);
testLayout.button1.WhenAction = THISBACK(StartProgress); // button1's WhenAction callback was not defined.
}
void TestWindow::StartProgress()
{
testLayout.Show(false);
testLayout2.Show(true);
testLayout2.progress.Set(0, 10);
Thread().Run(THISBACK(Progress));
}
void TestWindow::Progress()
{
// This thread code could be written in a much better way, but I'm simply giving you the idea :).
for(int i = 0; i < 10; i++)
{
if(Thread::IsShutdownThreads())
break;
Sleep(1000);
PostCallback([=]{
// Never directly call GUI elements. If wou want to do fancy GUI stuff with
// threads, use PostCallback.
testLayout2.progress++;
});
}
PostCallback([=]{
// Let us roll-back;
testLayout2.Hide();
testLayout.Show();
});
}
TestWindow::~TestWindow()
{
// Make sure that running threads terminate.
if(Thread::GetCount())
Thread::ShutdownThreads();
}
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: Wed, 16 August 2017 07:47] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: Code before Thread.Run() nor executed [message #48719 is a reply to message #48717] |
Mon, 28 August 2017 10:51 |
rafiwui
Messages: 105 Registered: June 2017 Location: Stuttgart, Germany
|
Experienced Member |
|
|
How exactly does GuiLock work? Because when I tried to implement it in my application it doesn't work. Is there a documentation?
EDIT:
This is part of the code I use:
void MainWindow::Generate()
{
// Generate
GuiLock __;
progress.progressMainLabel.SetText(t_("(0/3) Generating first..."));
if(!GenerateFirst())
{
AbortGeneration(1);
return;
}
progress.progressMain++;
progress.progressMainLabel.SetText(t_("(1/3) Generating second..."));
if(!Second())
{
AbortGeneration(2);
return;
}
progress.progressMain++;
progress.progressMainLabel.SetText(t_("(2/3) Generating third..."));
if(!GenerateThird())
{
AbortGeneration(3);
return;
}
progress.progressMain++;
progress.progressMainLabel.SetText(t_("(3/3) Done"));
}
Instead of showinf every step it just generates all three and at the end updates the gui. So what do I have to change that every step it is updated?
Greetings
Daniel
[Updated on: Mon, 28 August 2017 11:00] Report message to a moderator
|
|
|
|
|
|
Re: Code before Thread.Run() nor executed [message #48726 is a reply to message #48725] |
Tue, 29 August 2017 20:30 |
rafiwui
Messages: 105 Registered: June 2017 Location: Stuttgart, Germany
|
Experienced Member |
|
|
mirek wrote on Tue, 29 August 2017 20:26
You can call EnterGuiMutex() / LeaveGuiMutex() directly too...
Ok thanks.
mirek wrote on Tue, 29 August 2017 20:26
BTW, note that "__" is my way of saying "name does not matter".
Yeah I thought so
Greetings
Daniel
|
|
|
Goto Forum:
Current Time: Fri Sep 20 17:58:33 CEST 2024
Total time taken to generate the page: 0.02648 seconds
|
|
|