Posted by: George Soules
Using events works well if an asynchronous (non-blocking) call to a method that loads a document is acceptable. However, I have some cases where I need to make synchronous (blocking) calls to the document loading method.
For example, the app calls method X to update the HTML in documents "a", "b", and "c" one at a time in sequence (not in parallel). Method X needs to look something like this:
// "a" is done, ok to do "b"
// "b" is done, ok to do "c"
As such, method UpdateDoc must somehow wait until the onreadystatechange event fires before it can return to X.
A DoEvents spin-wait in UpdateDoc will work, but is not CPU-friendly. Somehow UpdateDoc needs to do a true thread sleep without suspending the code executed as a result of calling LoadDocument.
Since I don't know the magic behind mshtml and don't know who/what is executing between the time LoadDocument is called and the time the onreadystatechange fires, I'm at a loss as how to design the sleep-wait.
I tried putting UpdateDoc in it's own thread, but the thread appeared to exit after calling LoadDocument, but before the onreadystatechange event fired.
If I figure it out, I'll let everyone know how.