Clean messages in Windows Message Pump
Asked Answered
H

1

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

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

  1. How can I it?

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

  1. When are the other two threads created?

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.

Hypaesthesia answered 27/11, 2014 at 9:32 Comment(7)
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?Gildea
@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 concludedHypaesthesia
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.Gildea
@SriramSakthivel it always run on main thread then what is Reentracy here in my scenerio?Hypaesthesia
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 theDocumentCompleted 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.DoEventsGildea
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 3Hypaesthesia
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().Nullity
M
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

Mountainside answered 19/5, 2015 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.