title: WinForms: Open a messagebox from the WndProc method (Form class)
body: <p>I imagined that WndProc is called on another thread rather than main UI thread.</p>
  
  <p>I was right, so I thought that simply using InvokeRequired and Invoke on the form was enough to show a messagebox on the UI thread.</p>
  
  <p>I was wrong. And I don't understand why.</p>
  
  <p>How can I go around this problem?I'm looked around google but didn't find a solution.</p>
  
  <p>What I'm trying to do is simply raising a custom event (ClipboardUpdate) when clipboard changes</p>
  
  <p>the messagebox was just a test but didn't work, while just changing something like a string (a private field of the form) works, but it's not a good thing this behaviour because is a cross-thread operation in an unsafe way.</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>I don't have the code here because I created it on a friend's computer, however I can explain exactly what I wrote Because is short.
  I created a basic winform with visual studio, without anything.
  I used AddClipboardFormatListener (interop, but it's quite easy as a function, return int and accept IntPtr) function (on a windows 7 OS) to just detect WM_CLIPBOARDUPDATE message, inside winproc (a simple if, *if (e.Msg == ClipboardExtension.WM_CLIPBOARDUPDATE) DoClipboardUpdate();*).</p>
  
  <p>Now the DoClipboardUpdate do this:</p>
  
  <pre><code>if (InvokeRequired)
      Invoke(new VoidDelegate(OnClipboardUpdate));//Void delegate it's a delegate that doesn't take
      // Params and returns void
  else
      OnClipboardUpdate();
  </code></pre>
  
  <p>Quite easy right? OnClipboardUpdate just do this:</p>
  
  <pre><code>if (ClipboardUpdate != null) ClipboardUpdate(null,EventArgs.Empty);
  </code></pre>
  
  <p>ClipboardUpdate is an event declared in this way:</p>
  
  <pre><code>public event EventHandler&lt;EventArgs&gt; ClipboardUpdate;
  </code></pre>
  
  <p>In the end, the only method subscrived to ClipboardUpdate event has this inside:</p>
  
  <pre><code>MessageBox.Show("test");
  </code></pre>
  
  <p>What happens when I run the code? The event is triggered (I tried with an exception and it works) and the messagebox doesn't popup, however I can't interact anymore with my form because it behaves as if a popup was opened (this is the "normal" behaviour when you open a popup on a different thread, that's why I said that).</p>
  
  <p>Any suggestion on how to solve this?</p>