Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
What's the difference between DoEvents and BackgroundWorker?
Posted
Updated 27-Jan-11 6:35am
v4

They are 2 different things really. DoEvents just gives the current thread a chance to handle all pending messages. Ideally you should never have to use it. If you want the UI to be responsive, do all your heavy tasks in secondary threads and the BackgroundWorker makes it really easy to do this.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Jan-11 11:06am    
Good enough (a 5), but I added some more.
--SA
Espen Harlinn 27-Jan-11 14:15pm    
5+ Nice and simple :)
Nish Nishant 27-Jan-11 16:29pm    
Thank you.
Manfred Rudolf Bihy 27-Jan-11 16:47pm    
Good call! 5+
Nish Nishant 27-Jan-11 18:49pm    
Thanks.
Most important difference: DoEvents is not a multi-threading tools, it works in UI thread and passes all the messages blocking the caller for this time. That's why it usually don't resolve the problem some people hope to solve. Rule of thumb is never using it. (Manas is right.)

BackgroundWorker works in a different thread, good for many lengthy tasks without hanging the UI. Limitation: this technique is not well suited to spawn "infinite" tasks (of the same lifetime as the application). For this purpose, regular thread is better.

Thank you.
—SA
 
Share this answer
 
Comments
Nish Nishant 27-Jan-11 11:12am    
Voted 5. Good info here. Adds to existing answers.
Espen Harlinn 27-Jan-11 14:19pm    
5+ Good answer, DoEvents might be required when working with some ActiveX controls. When it is, it's usually a good time to look for alternatives though :)
Sergey Alexandrovich Kryukov 27-Jan-11 16:08pm    
Interesting note, Espen. To me, this is rather indication of ill design of those ActiveX controls; I would say, such design problems are characteristic for secondary layers of COM, IDispatch along is, well...
Espen Harlinn 27-Jan-11 16:19pm    
A common problem with this category of COM components is that the developer kept the source to himself and is currently out of business.
Sergey Alexandrovich Kryukov 27-Jan-11 16:50pm    
This is a problem of software assert management, not specifically COM. Not very rare.
short answer: Don't bother and DO NOT USE DoEvents()

long answer : backgroundworker is quite smart and advanced enough to handle cross thread.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jan-11 11:06am    
Good for short.
--SA
Manfred Rudolf Bihy 27-Jan-11 16:51pm    
Don't know who voted this down. 5+
Not as elaborated as Nishant's or SAkryukov's answer, but nevertheless right. :thumbsup:
DoEvent processes events queued on the UI thread. Except some rare cases, this is typically not something you should use.

BackGroundWorker does the work on a separate thread, so the UI keeps processing messages on it's own. You need to deal with thread synchronization, which is complex.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900