Hi what is the necessity of using Application.DoEvents and when we should use it?
Imho you should more less never use it, as you might end up with very unexpected behavior. Just generated code is ok. Things like you are executing again the event handler you are currently in,because the user pressed a key twice etc etc. If you want to refresh a control to display the current process you should explicitly call .Update on that control in instead of calling Application.DoEvents.
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.