I have a larger .NET Application where I get impossible stack traces of the form
...
System.Windows.Forms.Control.OnParentVisibleChanged(System.EventArgs)
System.Windows.Forms.Control.OnVisibleChanged(System.EventArgs)
System.Windows.Forms.Control.WmShowWindow(System.Windows.Forms.Message ByRef)
System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
DomainBoundILStubClass.IL_STUB_ReversePInvoke(Int64, Int32, Int64, Int64)
MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
System.Threading.Monitor.Enter(System.Object)
...
The Win32 Stack looks like this
...
System_Windows_Forms_ni+0x9e89d2
clr!UMThunkStub+0x6e
user32!UserCallWinProcCheckWow+0x2f8
user32!CallWindowProcW+0x8e
WindowsBase_ni+0x1632e2
WindowsBase_ni+0x323e42
clr!UMThunkStub+0x6e
user32!UserCallWinProcCheckWow+0x2f8
user32!DispatchClientMessage+0x9c
user32!_fnDWORD+0x33
ntdll!KiUserCallbackDispatcherContinue
win32u!NtUserPeekMessage+0x14
user32!_PeekMessage+0x43
user32!PeekMessageW+0x143
combase!CCliModalLoop::MyPeekMessage+0x53 [onecore\com\combase\dcomrem\callctrl.cxx @ 2925]
combase!CCliModalLoop::PeekRPCAndDDEMessage+0x49 [onecore\com\combase\dcomrem\callctrl.cxx @ 2607]
combase!CCliModalLoop::BlockFn+0x2c5 [onecore\com\combase\dcomrem\callctrl.cxx @ 2103]
combase!ClassicSTAThreadWaitForHandles+0xa5 [onecore\com\combase\dcomrem\classicsta.cpp @ 51]
combase!CoWaitForMultipleHandles+0x76 [onecore\com\combase\dcomrem\sync.cxx @ 123]
clr!MsgWaitHelper+0x8e
clr!Thread::DoAppropriateWaitWorker+0x2e6
clr!Thread::DoAppropriateWait+0x7d
clr!CLREventBase::WaitEx+0xb6
clr!AwareLock::EnterEpilogHelper+0x104
clr!AwareLock::EnterEpilog+0x62
clr!JITutil_MonEnterWorker+0x132
...
According to the docs PeekMessage will not pump and immediately return. But the call stacks above teach me otherwise. I am aware of the .NET concept of SynchronizationContext but I find it highly unusual that a random lock statement can cause application reentrancy. Does PeekMessage pump always WM_SHOW messages? What I could decipher is that during window close of a larger window hierarchy some child windows are notified with a (hide?) message which is pumped. The question is what can I do against this? Are there any guidelines out there how that should be handled?