Let me start with a small background of messages and messages.
On Windows, GUI applications work by processing messages. For example, when you move the mouse, Windows sends a WM_MOUSEMOVE message to the window under the mouse.
Windows sends a message to the "message queue" of the stream to which the window belongs, and someone has to direct this message to a specific window. This someone is a message pump.
Every Windows user interface infrastructure has a message pump, and the simplest message pump looks like this (this code example is in C ++, you can use interop to write it to .NET):
MSG msg; while(GetMessage(&msg, hwnd, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
Each GUI program must have a message pump, but the command line program can start the message pump by simply executing the code above. Obviously, since you do not have open windows, you will not receive any messages from the OS. To insert a message into the queue, use PostThreadMessage.
source share