1. 程式人生 > >WPF的TextBox拋出InvalidOperationException異常:Cannot close undo unit because no opened unit exists.

WPF的TextBox拋出InvalidOperationException異常:Cannot close undo unit because no opened unit exists.

man ack form framework select textbox -c ble ann

近期遇到一個問題。應用使用過程中突然崩潰,查看dump發現異常信息例如以下:UI dispatcher has encountered a problem:無法關閉撤消單元。由於不存在已打開的單元。

at MS.Internal.Documents.UndoManager.Close(IParentUndoUnit unit, UndoCloseAction closeAction) at System.Windows.Documents.ImmComposition.UpdateCompositionText(FrameworkTextComposition composition, Int32 resultLength, Boolean includeResultText, ITextPointer& start, ITextPointer& end)

at System.Windows.Documents.ImmComposition.RaiseTextInputStartEvent(FrameworkTextComposition composition, Int32 resultLength, String compositionString)
at System.Windows.Documents.ImmComposition.OnWmImeChar(IntPtr


從dump上能夠看到當時是在TextTox進行輸入時突然崩潰,可能和輸入法、UndoManager有關。
通過~*kb能夠看到全部線程的堆棧RetAddr : Args to Child : Call Site
000007fe`fd491430 : 006b0073`00690064 0075006c`006f0056 005c0031`0065006d 0064006e`00690057 : ntdll!ZwWaitForMultipleObjects+0xa
00000000`773a0740 : 00000000`23489cd0 00000000`23489cc0 00000000`00000000 00000000`00000000 : KERNELBASE!WaitForMultipleObjectsEx+0xe8
000007fe`eaedc96a : 00000000`00000000 000007fe`d788a580 00000000`1d9f6020 00000000`000018f0 : kernel32!WaitForMultipleObjects+0xb0

00000000`7742b990 : 00000000`23489f40 00000000`00000000 00000000`00000001 00000000`2348b120 : SogouPY+0x41a614
00000000`77592a4f : 00000000`23489f40 00000000`00000006 0000f67a`00000000 00000000`00000001 : kernel32!UnhandledExceptionFilter+0x160
00000000`774fd863 : 000007fe`d2640000 000007fe`f7d9e336 00000000`23490000 00000000`774fd8f5 : ntdll!LdrpLogFatalUserCallbackException+0x3f
00000000`774d905d : 00000000`23490000 00000000`2348a0f8 00000000`2348a080 00000000`2348a100 : ntdll!KiUserCallbackDispatcherHandler+0x20
00000000`774c8c0f : 00000000`23490000 00000000`77326388 000056fa`00012a98 00000000`2348dfe0 : ntdll!RtlpExecuteHandlerForException+0xd
00000000`774c9208 : 00000000`2348b120 00000000`2348aaf0 00000000`00000001 00000000`00000000 : ntdll!RtlDispatchException+0x45a
000007fe`fd49b3dd : 00000000`2348b258 00000000`00000001 00000000`00000005 00000000`042b2c00 : ntdll!RtlRaiseException+0x22f
000007fe`f7bc565b : 00000000`2348b258 00000000`2348b198 00000000`050ac370 00000000`050bd478 : KERNELBASE!RaiseException+0x39
000007fe`f7ad18e7 : 00000000`042cfe08 00000000`050ac370 000007fe`d2ad5b05 00000000`005946b8 : clr!RaiseTheExceptionInternalOnly+0x28b
000007fe`d2ad5b40 : 000007fe`d26b1fe8 00000000`000000ab 00000000`005946b8 00000000`2348b900 : clr!IL_Rethrow+0x98
000007fe`f7bc6635 : 00000000`228c1b10 000007fe`d26b1fe8 00000000`00594620 00000000`228c1b10 : WindowsBase_ni+0x495b40
000007fe`f7bc6d6c : 00000000`00594620 000007fe`d2ad5b05 00000000`2348dfe0 00000000`005946b8 : clr!ExceptionTracker::CallHandler+0xc5
000007fe`f7bc6c1f : 00000000`2348dfe0 00000000`2348b740 00000000`2348d430 00000000`00000002 : clr!ExceptionTracker::CallCatchHandler+0x78
00000000`774d90dd : 00000000`2348d500 00000000`2348dfe0 00000000`00000000 00000000`2348b740 : clr!ProcessCLRException+0x2e2

從上面的dump信息能夠看到當時有可能是用到搜狗輸入法。
接下來我們就是要分析當時輸入的是哪個字符,還是通過!dso查看當時全部的堆棧信息0:012> ~12e !dso
OS Thread Id: 0x2080 (12)
RSP/REG Object Name
000000002142AD48 0000000002c94188 System.Text.UTF8Encoding
000000002142AEE0 00000000031de548 System.Windows.Threading.Dispatcher
000000002142AFF0 00000000031de880 System.Windows.Threading.DispatcherUnhandledExceptionEventArgs
000000002142B660 00000000031de548 System.Windows.Threading.Dispatcher
000000002142B678 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142B680 00000000031dee38 MS.Win32.HwndSubclass+DispatcherOperationCallbackParameter
000000002142B6B0 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142B6B8 00000000031de548 System.Windows.Threading.Dispatcher
000000002142B888 0000000002cd1ad0 System.RuntimeType
000000002142B980 00000000031de548 System.Windows.Threading.Dispatcher
000000002142B998 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142B9A0 00000000031dee38 MS.Win32.HwndSubclass+DispatcherOperationCallbackParameter
000000002142B9D0 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142B9D8 00000000031de548 System.Windows.Threading.Dispatcher
000000002142BBB0 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142BBC8 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142BBD0 00000000031dee38 MS.Win32.HwndSubclass+DispatcherOperationCallbackParameter
000000002142BC00 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142BC08 00000000031de548 System.Windows.Threading.Dispatcher
000000002142C148 0000000002cd1ad0 System.RuntimeType
000000002142C280 00000000031de548 System.Windows.Threading.Dispatcher
000000002142C298 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142C2A0 00000000031dee38 MS.Win32.HwndSubclass+DispatcherOperationCallbackParameter
000000002142C2D0 00000000035b19b8 System.Windows.Threading.DispatcherOperationCallback
000000002142C2D8 00000000031de548 System.Windows.Threading.Dispatcher
000000002142C658 0000000002cd1ad0 System.RuntimeType
000000002142CB28 0000000002cd1ad0 System.RuntimeType
000000002142CF08 000000000366af28 System.Windows.Documents.TextSelection
000000002142CF10 000000000366acb8 System.Windows.Documents.TextContainer

000000002142D058 0000000002c8ddc8 System.String zh-CN
000000002142D078 00000000032b4d70 System.Char[]
000000002142D578 000000000366ad68 System.Windows.Documents.TextEditor
000000002142D5A0 000000000366af28 System.Windows.Documents.TextSelection
000000002142D5C8 000000000366ad68 System.Windows.Documents.TextEditor
000000002142D5E0 00000000030ee118 System.Windows.Input.InputManager
000000002142D608 000000000378e350 System.Windows.Documents.ImmComposition
000000002142D630 000000000378e350 System.Windows.Documents.ImmComposition
000000002142D640 000000000378e350 System.Windows.Documents.ImmComposition
000000002142D668 000000000510f550 System.Windows.Documents.FrameworkTextComposition

000000002142D680 000000000510f530 System.String ,
從上面dump信息能夠看出當時的string有zh-cn以及“ 。”。 能夠推測當時應該是輸入法當時是在中文輸入狀態下輸入了“ 。”, 導致TextBox崩潰。
結論:通過上述信息試著在應用上進行重現, 發現終於原因是:先切換到英文輸入法。輸入字符到TextBox到最大長度,然後切換輸入到中文輸入法,選擇搜狗輸入法,然後敲入字母,然後按“,”。掛掉了。

推測是TextBox已經到了最大長度。對於搜狗輸入法在中文情況“,”就是回車或者確認的意思,然後IME把這些字符扔給了控件,控件由於到了最大長度就又觸發了Undo。
解決的方法:把TextBox的UndoLimit設置為0,或者IsUndoEnabled設置為False, 兩者都能夠禁用Undo功能, 從而避免上述異常。



WPF的TextBox拋出InvalidOperationException異常:Cannot close undo unit because no opened unit exists.