Wt::WTextArea data update in detached thread
Added by Stefano Fattore about 2 years ago
Hello, I'm new to the forum, I hope I'm not wrong in posting here.
I'm developing a web application in Wt 4.9.1, I've come across a situation where I have to perform an important task (such as executing a code external to the application) that takes quite a long time.
I thought about running this task in a separate thread and capturing its stdout to pipe it into a WTextAreas.
I tried via Wt::WServer::post(...)
passing the sessionID and a code to execute like in this example:
`void Dispatcher::run_src()
{
...
Wt::WServer::instance()->post(
WApplication::instance()->sessionId(),
[=] {
// C99 functions
begin_emulator(CompAndRun.bin.toUTF8().c_str());
while (emulate());
end_emulator();
// end C99 functions
reg_rend_->update(machine_status()); /* aka register_render (object that contains the WTextAreas), an instance variable of Dispatcher */
}
);
...
}`
machine_status() returns a char pointer containing a Json, if I try to print its correct content but when invoking reg_rend_->update(machine_status()) which calls an update the content of multiple WTextAreas based on the parsing of the json I get:
[error] "Wt: Exception in WApplication::notify(): boost::bad_get: failed value get using boost::get"
I also tried not using the lambda function or using std::thread but to no avail.
I think I'm getting the concept of how things work here wrong, I'm going crazy to find the solution, how can I do?
Finally, I apologize if my English is bad. Thank you.
Replies (2)
RE: Wt::WTextArea data update in detached thread - Added by Roel Standaert about 2 years ago
When you use WServer::post()
, the lambda will be executed while the WApplication
's UpdateLock
is held. You don't want to hold the UpdateLock
while you're doing your long-running task.
One approach, detailed in the serverpush
example, is to run your work in a separate thread, and directly grab the UpdateLock
whenever you want to update the UI.
Similarly, you can also use post(...)
whenever you want to update the UI.
P.S.: I see you accidentally posted this twice, probably mistakenly because Redmine was being slow or something. I removed the other one.
RE: Wt::WTextArea data update in detached thread - Added by Stefano Fattore about 2 years ago
Yes I mistakenly clicked the send button twice, tried to remove a copy of the post without success.
Thank you for your quick reply, I will look into the example you gave me.
Thanks again.