10

I don't know much about Windows Message Pump but i guess events are triggered using Message Pump.

1) When my Web browser control navigates to some websites it creates different events of Document Completion. Once I have what I need in WebBrowser_Document_Completed() I want to ignore all further Document Completion.

How can I do it?

2) If I show a MessageBox() in Document_Completed(...), It shows multiple message boxes, so it is running on parallel threads, but when I debug it I find that it runs always on main thread.

When are the other two threads created?

3) Also, when I press Close it closes the window but the process is still running in the background. I am not using any other thread yet I still see two other threads when I debug.

pnuts
  • 54,806
  • 9
  • 74
  • 122
Charlie
  • 4,262
  • 1
  • 28
  • 50
  • As soon as you get the first event, you can unsubscribe the event, or use a flag to ignore the event? Btw how did you conclude there are multiple threads? – Sriram Sakthivel Nov 27 '14 at 09:35
  • @SriramSakthivel Program does not go further if Dialog Result is not returned ,if it runs on Same thread ,then how multiple Message box are shown simultaniously ,That how i concluded – Charlie Nov 27 '14 at 09:38
  • Reentracy can cause this, so technically single thread can do this. Display the `Thread.CurrentThread.ManagedThreadId` in messagebox to confirm that really they are different threads. – Sriram Sakthivel Nov 27 '14 at 09:41
  • @SriramSakthivel it always run on main thread then what is Reentracy here in my scenerio? – Charlie Nov 27 '14 at 09:46
  • 2
    While waiting on `MessageBox.Show` or something modal, winforms will internally dispatches the message queue and processes the messages, eventually some message in the queue caused the same main thread to again invoke the`DocumentCompleted` event(even when you're still on the MessageBox.Show). Reentracy is somewhat like recursion, but the difference is you didn't called the method yourself as opposed to recursion(you call it deliberately). Same like [Application.DoEvents](http://stackoverflow.com/questions/5181777/use-of-application-doevents/5183623#5183623) – Sriram Sakthivel Nov 27 '14 at 09:51
  • Thanks Alot that helped me, but i see two more threads that are not in sleeping or wait state ,Maybe they cause the closing problem ,point no 3 – Charlie Nov 27 '14 at 09:58
  • 9
    MessageBox is dangerous, it pumps a message loop itself so allows this kind of re-entrancy. The DocumentCompleted event can fire again, giving you another message box. That makes it a very miserable way to debug code, just use a debugger breakpoint or Debug.WriteLine(). – Hans Passant Nov 27 '14 at 09:58

1 Answers1

1

The most direct way to do this will be to intercept and evaluate messages being propagated to your control by overriding it's WndProc() method. With a little bit of debugger observation you can identify the wm_message constant that corresponds to your target event and then alter execution flow accordingly. There is a pretty good example of this on the MSDN site: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

To get an idea of the message(s) you are seeing/looking for, reference: http://www.pinvoke.net/default.aspx/Constants/WM.html

Serialize
  • 471
  • 6
  • 13