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

來源:互聯網
上載者:User

標籤:狀態   res   資訊   堆棧   斷行符號   win   font   tle   enabled   

近期遇到一個問題。應用使用過程中突然崩潰,查看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進行輸入時突然崩潰,可能和IME、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資訊能夠看到當時有可能是用到搜狗IME。
接下來我們就是要分析當時輸入的是哪個字元,還是通過!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以及“ 。”。 能夠推測當時應該是IME當時是在中文輸入狀態下輸入了“ 。”, 導致TextBox崩潰。
結論:通過上述資訊試著在應用上進行重現, 發現終於原因是:先切換到英文IME。輸入字元到TextBox到最大長度,然後切換輸入到中文IME,選擇搜狗IME,然後敲入字母,然後按“,”。掛掉了。

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



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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.