How to see windows messages of a dialog shown with a second thread?

c#, messaging, multithreading, winforms

Solution

Pretty much all Application methods, including AddMessageFilter, are thread specific. So, if you want to filter the messages on the other thread you have to call AddMessageFilter on that thread.

Just change your Run method to:

void Run()
{
    using( MyDialog dialog = new MyDialog() )
    {
        Application.AddMessageFilter(new MyMessageFilter());
        Application.Run(dialog); 
    }
}

Problem

I've registered a message filter using ``` Application.AddMessageFilter(m_messageFilter); ``` using this I can log all of the mouse clicks a user makes in the application's user interface. However, one dialog is run on a separate thread, with code something like: ``` void Run() { using( MyDialog dialog = new MyDialog() ) { dialog.ShowDialog(); } } Thread thread = new Thread(Run); ``` The message filter I set up doesn't get to see messages that go to this window. How can I get them (ideally without being too intrusive)? I tried to override MyDialog.PreProcessMessage, but I'm confused that this never seems to get called. Thanks.

Original source