Is there only one “message pump” or many?

I'm new to .NET and Windows, and I'm trying to figure out how to integrate legacy code into my C # project. The guy owning the code said he only needs a “message pump” and a “processing downtime”.

After some googling, does at least MFC sound, do Windows Forms and Win32 all have their own message pumps? It's true?

My C # code is a console application which, as I understand it, has no message pump. I thought it was just GUI stuff (?).

So what do I need to do in my C # code so that its old code works when he says he needs a “message pump”?

The code I need to use was used in a GUI application. Now I need to use it on the server and was a little scared of the need to use a GUI message pump. My code is pure C # without any GUI tools in it at all. Just the number of crunches and I / O.

+4
source share
3 answers

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.

+8
source

To start the message pump in .NET , call Application.Run(); . This is a blocking call that will be completed only if the program receives an exit message. (Usually if the system shuts down). It is usually used by programs that need to wait for a specific event. (And, of course, using regular graphical applications.)

Then you can handle the Application.Idle event.

If you need to process Windows messages, you can create a hidden form, override its WndProc method, and pass it to Application.Run .

You can do all this even in the server process.

+5
source

You're right. In Windows, communication pumps are "GUI things", although they are used much more than that (hotkey notification, etc.).

The process may have a threaded message pump. Usually they are created when you create your first window. Console applications have no messages. You can get around this by creating a Windows Forms window and making it invisible.

When you call Application.Run , Windows Forms will start your message pump. You can pass this HWND of this window, and he will probably know what to do with it. If you want to catch messages sent back, you can override the WndProc forms. This will only catch messages sent to this window, though ...

+2
source

Source: https://habr.com/ru/post/1286001/


All Articles