Application.Exit没有杀死消息泵?

时间:2008-12-04 13:31:20

标签: .net wpf winforms windbg

我有线程异常处理程序,它保存异常堆栈跟踪并应关闭应用程序。我打电话给Applicatoin.Exit,但只关闭窗口,让应用程序无窗口运行。

我知道,这通常是因为某些后台线程仍在运行。我将windbg附加到无窗口进程,似乎只有两个托管线程,其中一个看起来像gc终结器线程,第二个看起来像消息泵???任何人都明白这一点?

调用Application.Exit和用户关闭主窗口之间有区别吗?

0:005> !threads
ThreadCount: 2
UnstartedThread: 0
BackgroundThread: 1
PendingThread: 0
DeadThread: 0
Hosted Runtime: no
                                      PreEmptive   GC Alloc           Lock
       ID OSID ThreadOBJ    State     GC       Context       Domain   Count APT Exception
   0    1  284 002e9668      6020 Enabled  021a7268:021a7fe8 002e4c68     0 STA
   2    2  d48 002f9890      b220 Enabled  00000000:00000000 002e4c68     0 MTA (Finalizer)
0:001> ~0 s
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Windows\system32\MSCTF.dll - 
eax=02162530 ebx=002e9668 ecx=02162530 edx=02162530 esi=0019ef9c edi=0019ee3c
eip=77589a94 esp=0019edcc ebp=0019ede8 iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000246
ntdll!KiFastSystemCallRet:
77589a94 c3              ret
0:000> !clrstack
OS Thread Id: 0x284 (0)
ESP       EIP     
0019ef24 77589a94 [ComPlusMethodFrameGeneric: 0019ef24] MS.Win32.UnsafeNativeMethods+ITfMessagePump.GetMessageW(System.Windows.Interop.MSG ByRef, Int32, Int32, Int32, Boolean ByRef)
0019ef44 56d61937 System.Windows.Threading.Dispatcher.GetMessage(System.Windows.Interop.MSG ByRef, IntPtr, Int32, Int32)
0019ef90 56d617e3 System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
0019efe0 56d616c7 System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
0019effc 56d6162d System.Windows.Threading.Dispatcher.Run()
0019f008 5533ddb0 System.Windows.Application.RunInternal(System.Windows.Window)
0019f034 5533dbe5 System.Windows.Application.Run(System.Windows.Window)
0019f044 5533d836 System.Windows.Application.Run()
0019f04c 01ea00ad UI.App.Main()
0019f268 79e7c74b [GCFrame: 0019f268] 

1 个答案:

答案 0 :(得分:10)

我解决了这个问题。我应该调用System.Windows.Application.Shutdown()而不是调用System.Windows.Forms.Application.Exit()。消息泵线程属于wpf,您可以从该线程的堆栈跟踪中看到它。

换句话说,System.Windows.Forms.Application.Exit()不会关闭WPF消息泵。