What is the necessity of using Application.DoEvents
and when we should use it?
Application.DoEvents
is usually used to make sure that events get handled periodicaly when you're performing some long-running operation on the UI thread.
A better solution is just not to do that. Perform long-running operations on separate threads, marshalling to the UI thread (either using Control.BeginInvoke
/Invoke
or with BackgroundWorker
) when you need to update the UI.
Application.DoEvents
introduces the possibility of re-entrancy, which can lead to very hard-to-understand bugs.