C # threads - sending messages between threads

I am working on a project that includes reuse as well as porting some existing MFC codes to C #.

The current code in MFC creates some threads and uses :: PostthreadeMessage () and ON_THREAD_MESSAGE (msg, func) for asynchronous communication between threads through messages.

:: PostthreadeMessage () → sends a message to a specific thread identifier. ON_THREAD_MESSAGE (msg, func) → calls the function (func) when a message is received (msg).

Since I am not very well versed in streaming interfaces in C #, I tried to find a similar API in C #, but could not find.

It would be great if someone can help me find the appropriate functions for this purpose in C #

+5
source share
3 answers

Here is the C # MVP tip by Nicholas Paldino :

If you send a message to a stream and not to a specific window, then you will have to have something that implements the IMessageFilter interface, and then pass this implementation to the static AddMessageFilter Method on the Application Class, so it can process the message that comes to the thread.

From another thread, you will have to use the PostThreadMessage API method through the P / Invoke layer.

+1
source

I would advise you to use the direct port for this (cross-threading) part of the application. Maybe you can check if you can use delegates and events. A delegate can be registered at startup, and these delegates (callbacks) can be called in place of the user's message.

+1

#, .NET 4, ConcurrentQueue<T> , .

+1

All Articles