I'm using the IWebBrowser2 interface to render a page from an HTML string created at runtime. I have written a method (let's call it DisplayHtmlString) that takes an HTML string and renders it as shown in this example. The method also calls Navigate2 with "about:blank" first, to ensure a document is present, and it also calls close after calling write.
The first time I call DisplayHtmlString, the page is always rendered correctly, i.e. the browser displays HTML according to the string I pass. The problem is that subsequent calls sometimes do not work correctly, but render a blank page instead. What could be causing this?
I have found out that when the blank page is shown, this is the result of navigating to about:blank. This was determined by navigating to a local file instead, which is then shown (whereas the HTML string should be shown instead, due to the subsequent write/close). So the call to Navigate2 works, while the calls to write and close sometimes don't.
I considered IE-internal security checks as a possible cause (cross-domain checking?), but my gut feeling is that this isn't what's happening here.
It seems more likely to me that it is some kind of synchronization problem, along the lines of "IE hasn't finished rendering yet before the next call to DisplayHtmlString comes along". My code originally didn't check the browser's ready state (because the example doesn't). I added an experimental waiting loop with a call to get_readyState and observed that the state never got beyond "loading" before returning from the method - probably because rendering is asynchronous(?). I also notice that when successive calls to DisplayHtmlString work correctly, the program's main message loop has run (giving Windows a chance to process messages), which is not the case in the scenario where successive calls to DisplayHtmlString fail.
So I'm pretty sure I need to provide correct synchronization here, but how? I notice there's a method named onreadystatechange, but haven't experimented with that yet, due to the multitude of other things I tried while groping in the dark. Could that be the solution, and how does one use it correctly? Or, alternatively, shall I just process the message loop inside DisplayHtmlString until the ready state changes to "complete"?
UPDATE: Added message loop processing to DisplayHtmlString. In the first call (which works), the ready state gets to "interactive", but no further (which doesn't seem to be a problem). In the subsequent call (when it fails), the ready state stays at "loading", even though the message loop is processed.