Josh Kelley
Josh Kelley

Reputation: 58342

TWebBrowser and ProcessMessages

Sample code for working with TWebBrowser typically inserts a loop to call Application.ProcessMessages until ReadyState is READYSTATE_LOADED.

For example, from the Embarcadero forums, some code to load an HTML string into a TWebBrowser:

mWebBrowser->Navigate(L"about:blank");
while (mWebBrowser->ReadyState< READYSTATE_LOADED)
   Application->ProcessMessages();

// load mWebBrowser from TStreamAdapter

As I understand it, calling ProcessMessages is risky, because it can create reentrancy problems. (For example, if the user clicks the Close button on the form, then that message could get handled by ProcessMessages, such that the TWebBrowser instance no longer even exists when the function returns.)

Am I understanding this correctly?

If so, is there a "safe" way to handle TWebBrowser's needs to have messages processed, without ProcessMessages' risks? Or do I need to redesign all of my TWebBrowser code to be asynchronous?

Upvotes: 2

Views: 511

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

You don't need to call ProcessMessages at all. You can let the main application message loop deal with the messages.

But the code here is waiting for a page to load before doing the next task. To retain that sort of behaviour you switch to an asynchronous event driven approach. Remove the loop and instead handle the browser's OnDocumentComplete event. Only then do you load the actual content from your stream.

Upvotes: 5

Related Questions