I have a thread that reads messages from a named pipe. It is a blocking read, which is why it’s in its own thread. When this thread reads a message, I want it to notify the Windows Forms message loop running in the main thread that a message is ready. How can I do that? In win32 I would do a PostMessage, but that function does not seem to exist in .Net (or at least I could not find it).
Share
In WinForms you can achieve this with Control.BeginInvoke. An example:
A cleaner variant of the above which doesn’t depend on WinForms would be to use
SynchronizationContext. Call SynchronizationContext.Current on your main thread, and then pass that reference to the constructor of the class shown below.