Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Application.DoEvents()

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.

like image 299
Nathan Tornquist Avatar asked Jul 19 '11 14:07

Nathan Tornquist


People also ask

What is application DoEvents in C#?

Application. DoEvents() can be used to process the messages waiting in the queue on the UI thread when performing a long-running task in the UI thread. This has the benefit of making the UI seem more responsive and not "locked up" while a long task is running.

What is application DoEvents in VB net?

Application. DoEvents , your form repaints when another window is dragged over it. If you remove My. Application. DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.


1 Answers

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()...

like image 141
Jalal Said Avatar answered Sep 27 '22 17:09

Jalal Said