tags:

views:

1095

answers:

4

I'm new to .net and windows and are 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 "idle processing".

After some googling It sounds that at least MCF, Windows Forms and Win32 all have their own message pumps? Is this true?

My C# code is a console app which I understand does not have any message pump at all, thought those were just GUI things?

So, what do I need to do in my C# to make his old code work when he says he need a "message pump"???

Edit: The code I need to use have been used in a GUI. Now I need to use it in a server instead and got a bit terrified about the need for a GUI message pump, my code is pure C# with no GUI stuff in it at all. Just some number crunching and I/O.

+1  A: 

There are message pumps (see: Message_pump) in loads of proceses.

What exactly are you trying to achieve?

It sounds to me like you either need more information from the guy who owns the code, or you need him to do it.

Bravax
I need to call his code to do some stuff, his code interacts with external hardware. And also need to listen on some kind of callbacks from his code in order to get data from this external hardware.
+4  A: 

Let me start with a little background on messages and message pumps.

In Windows GUI applications work by handling messages, for example when you move the mouse Windows sends a WM_MOUSEMOVE message to the window under the mouse.

Windows posts a message to the "message queue" of the thread that the window belongs to and someone has to route this message to the specific window, that someone is the message pump.

Every Windows UI framework has a message pump, the simplest message pump looks like this (in C++, you can use interop to write it in .net):

MSG msg;
while(GetMessage(&msg, hwnd, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

every GUI program must have a message pump but a command line program can start a message pump just by running the code above, obviously, since you don't have open windows you will not get any messages from the OS, to insert a message into the queue use PostThreadMessage.

Nir
Take a look at the links here:http://msdn.microsoft.com/en-us/library/dd458656(VS.85).aspxAlso - idle handling is the notion of calling a helper function often called OnIdle when the mesaage queue is empty - normally routines below Onidle should return quickly so as to avoid blocking the pump. Typically it can be used to check for completion of background tasks etc.
morechilli
Elk, no you can't make sure, he may want to open windows or he may need some special feature of the WinForms (or MFC or whatever) message pump you just can't know - but you can get it to work the usual way, debugging and working around his quirks.
Nir
A: 

You're right, in windows message pumps are "GUI things" although they are used for much more than that (hotkey notification etc).

A process can have a message pump per thread. Usually these are created when you create your first window. Console apps don't have message pumps. You could get around this by creating a WinForms window and making it invisible. When you call Application.Run winforms will start your message pump. You can pass him the HWND of that window and he'll probably know what to do with it. If you want to catch messages sent back you can override the forms wndproc. This will only catch messages sent to that window though..

Mendelt
+1  A: 

To run a message pump in .Net, call Application.Run();. This is a blocking call that will only finish running if the program receives a quit message. (Usually, if the system is shutting down). It is typically used by programs that need to wait for a specific event. (And, of course, by regular GUI apps)

You can then handle the Application.Idle event.

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

You can do all of this even in a server process.

SLaks
Ok, great. So If I'm using this old code (I think it could be a COM component using MFC) it will work nicely if I spawn a thread in my .Net app which will do a Application.Run()?I have my own number crunching main loop in an other thread doing other stuff and calling upon this old library once in a while.I suppose there's code in this library which need to be run on MFC's OnIdlde (If this is indeed MFC code), will that too be taken care of in Application.Run?
I don't know; it depends what the component is doing and how. It probably isn't thread safe. Please note that to run a message loop on a thread, it must be an STA (Single Threaded Apartment) thread; call Thread.SetApartmentState before starting the thread. (Or, for the main thread, add [STAThread] before the Main() method
SLaks