Search code examples
c#multithreading.net-cf-3.5

What is the effect of calling Application.DoEvents() on a background thread?


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.


Solution

  • 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.