Project

General

Profile

The Widgets do not update Text Instantly

Added by Danish Jalil about 9 years ago

I have set up a Rest API which I am trying to hit from this simple Web App. I am trying to show the text on the screen when I get a response.

In the code that I have attached I am updating the text in the TextArea by calling the setText method. The text only gets updated when I click again on the submit button. Is it because I get the response in another thread that can not update the widgets?

Any help will be much appreciated!


Replies (1)

RE: The Widgets do not update Text Instantly - Added by Wim Dumon about 9 years ago

Your assumption is correct: handleRequest is executed in an asynchronous thread, from which you can not just edit the widget tree. There are two steps that you need to resolve the situation: (1) enable server push and push your changes, and (2) you must grab a lock on the widget tree before you modify it, or alternatively use WServer::post() to modify the widget tree (in which case Wt will take care of grabbing the lock).

See documentation of WApplication (section on server push + WApplicationLock) and WServer (about post()). There is also an example (feature/serverpush) that demonstrates this functionality.

I recommend to work with WServer::post(), since it is usually simpler than properly managing the lock.

Best regards,

Wim.

    (1-1/1)