5

I am developing a messaging system based on webBrowser controls so that I can format the text however I please. When the user is offline and is sent messages, the messages are stored and an event is sent for every message when they log back in. When I set the default html and such for the website, I normally use:

while (this.webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();

This works when the program is running normally. When the user is receiving message sent when they are offline, this triggers the next message event and so on, with each message until the last one. This means that the last message sent when they are offline is the only one displayed. I'd like something like Application.DoEvents() that allows the control to keep updating and loading, but that doesn't trigger other events.

Thanks

EDIT:

I fixed the problem by removing DoEvents() completely. Instead of changing the DocumentText as I worked, I set up a string builder and then set the html all at once at the end.

Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72
  • 2
    If you are calling `Application.DoEvents()`, normally that indicates a fault in your design. Is there any reason you cannot use the 'completed' event? – leppie Jul 19 '11 at 14:24
  • I essentially need the web browser to finish loading, but the current thread to keep running. I format the message and then check the backing html. If the html is wrong, I update that, and then add the message. Before I ever change the document text I make sure that the web browser is loaded and is at the complete readystate. – Nathan Tornquist Jul 19 '11 at 14:27

2 Answers2

7

I recommend you to stop using Application.DoEvents(), it is generate problems more than it solves. check this.

A better way is either use an AutoResetEvent to notify whenever loading completed, or by raising an event whenever the loading is done. also you can run your waiting on another thread so don't have to use Application.DoEvents()...

Community
  • 1
  • 1
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • That was an excellent article. Really, I'd just like to allow the webBrowser control to finish loading and then continue. I don't care if the user interface is affected because the code should run fast enough to avoid any isses. The way DoEvents() works though, I know that isn't even close to the solution. I would like to avoid going to other events though. I want to stay within the running method if I can. – Nathan Tornquist Jul 19 '11 at 15:07
  • 1
    This didn't solve my problem but I am accepting the answer because of the link about DoEvents(). The information that provided was enough for me to rewrite the program. – Nathan Tornquist Jul 20 '11 at 16:13
0

You can use System.Threading.Thread.Join() as an alternative to DoEvents(), since this method halts your current thread execution until the preceding call thread completes execution.

Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55