2

Just as the question asks: What does Application.DoEvents() do when called on a background thread?

To give some context, I'm reviewing a rather complex solution written (not by me) for the .NET CF in C#. The reason I'm reviewing it is that it has some inherent problems that I've been asked to investigate.

One rather interesting tidbit is that the app creates a long-running background thread on startup which enters a timed loop. It ends up calling Application.DoEvents() on each loop iteration.

I can't quite figure out what the effect of this would be - does it flush the message queue on the application's Main thread? Or does it flush the message queue on the thread on which it was called (even though a background thread won't have a queue to flush).

It's almost certainly the cause of some otherwise unexplained application behaviour.

pdriegen
  • 2,019
  • 1
  • 13
  • 19
  • The accepted answer in [this SO post](http://stackoverflow.com/questions/6267001/does-doevents-effect-only-the-current-thread) addresses your question. – dotNET Oct 23 '14 at 13:00

1 Answers1

4

Application.DoEvents() processes all Windows messages currently in the message queue, which is one per thread that has created a window. So if you call it on a "background thread", it'll do nothing unless you created a window on that thread.

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122