Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send or post a message to a Windows Forms message loop

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).

like image 823
dan-gph Avatar asked Sep 30 '09 13:09

dan-gph


People also ask

What is message loop in Windows programming?

The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window. Usually only the first thread creates windows.

What is the difference between GetMessage () and DispatchMessage () function?

GetMessage pulls the WM_LBUTTONDOWN message from the queue and fills in the MSG structure. Your program calls the TranslateMessage and DispatchMessage functions. Inside DispatchMessage, the operating system calls your window procedure. Your window procedure can either respond to the message or ignore it.

What is the difference between GetMessage and PeekMessage?

The main difference between the two functions is that GetMessage does not return until a message matching the filter criteria is placed in the queue, whereas PeekMessage returns immediately regardless of whether a message is in the queue.


2 Answers

PostMessage (and likewise SendMessage) are Win32 API functions, and thus are not directly associated with .NET. .NET does however have good support for interoping with the Win32 API, using P/Invoke calls.

As it seems you are new to doing Win32 programming .NET, this MSDN Magazine article provides a solid introduction on the topic.

The excellent pinvoke.net website details how to use many of these API functions from C#/VB.NET. See this page for PostMessage specifically.

The standard declaration is the following:

[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

But as the page indicates, it is wise to wrap this function to handle Win32 errors properly:

void PostMessageSafe(HandleRef hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
    bool returnValue = PostMessage(hWnd, msg, wParam, lParam);
    if(!returnValue)
    {
        // An error occured
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }
}        
like image 176
Noldorin Avatar answered Sep 18 '22 04:09

Noldorin


In WinForms you can achieve this with Control.BeginInvoke. An example:

public class SomethingReadyNotifier
{
   private readonly Control synchronizer = new Control();

   /// <summary>
   /// Event raised when something is ready. The event is always raised in the
   /// message loop of the thread where this object was created.
   /// </summary>
   public event EventHandler SomethingReady;

   protected void OnSomethingReady()
   {
       SomethingReady?.Invoke(this, EventArgs.Empty);
   }

   /// <summary>
   /// Causes the SomethingReady event to be raised on the message loop of the
   /// thread which created this object.
   /// </summary>
   /// <remarks>
   /// Can safely be called from any thread. Always returns immediately without
   /// waiting for the event to be handled.
   /// </remarks>
   public void NotifySomethingReady()
   {
      this.synchronizer.BeginInvoke(new Action(OnSomethingReady));
   }
}

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.

public class SomethingReadyNotifier
{
    private readonly SynchronizationContext synchronizationContext;

    /// <summary>
    /// Create a new <see cref="SomethingReadyNotifier"/> instance. 
    /// </summary>
    /// <param name="synchronizationContext">
    /// The synchronization context that will be used to raise
    /// <see cref="SomethingReady"/> events.
    /// </param>
    public SomethingReadyNotifier(SynchronizationContext synchronizationContext)
    {
        this.synchronizationContext = synchronizationContext;
    }

    /// <summary>
    /// Event raised when something is ready. The event is always raised
    /// by posting on the synchronization context provided to the constructor.
    /// </summary>
    public event EventHandler SomethingReady;

    private void OnSomethingReady()
    {
        SomethingReady?.Invoke(this, EventArgs.Empty);
    }

    /// <summary>
    /// Causes the SomethingReady event to be raised.
    /// </summary>
    /// <remarks>
    /// Can safely be called from any thread. Always returns immediately without
    /// waiting for the event to be handled.
    /// </remarks>
    public void NotifySomethingReady()
    {
        this.synchronizationContext.Post(
                state => OnSomethingReady(),
                state: null);
        }
    }
like image 40
Wim Coenen Avatar answered Sep 22 '22 04:09

Wim Coenen