2017-05-12 48 views
0

我很困惑這實際上是如何實現的。當從ClipBoard中獲取數據時發生死鎖(GetDataObject)

在我的應用程序(.NET Framework 4.0中)我有一個 「粘貼」 按鈕。在應用程序啓動時或焦點對象更改時,我會檢查剪貼板中的數據以設置該按鈕的啓用狀態。

現在,它的發生幾次(一臺機器上),這個功能似乎死鎖。看看堆棧跟蹤:

at System.StubHelpers.InterfaceMarshaler.ConvertToManaged(IntPtr pUnk, IntPtr itfMT, IntPtr classMT, Int32 flags) 
at System.Windows.Forms.UnsafeNativeMethods.OleGetClipboard(IDataObject& data) 
at System.Windows.Forms.Clipboard.GetDataObject(Int32 retryTimes, Int32 retryDelay) 
at System.Windows.Forms.Clipboard.GetDataObject() 
at XXX.Forms.ClipboardHelper.<GetClipboardType>b__5() 
at XXX.DataProvider.Executor.Execute[TResult](Logger logger, LogLevel level, Func`1 action, Boolean throwUp, TResult defaultValue, String format, Object[] arguments) 
at XXX.DataProvider.Executor.Catch[TResult](Logger logger, LogLevel level, Func`1 action, TResult defaultValue, String format, Object[] arguments) 
at XXX.DataProvider.Executor.Catch[TResult](Logger logger, Func`1 action, TResult defaultValue, String format, Object[] arguments) 
at XXX.Forms.Ribbon.RibbonFormHelper.IsPasteButtonEnabled() 
at XXX.Forms.StartForm.XXX.Forms.IClipboardViewer.EnablePasteButton() 
at XXX.Forms.Ribbon.RibbonFormHelper.OrderOverview_FocusChangedEvent(Object sender, EventArgs e) 
at DevExpress.XtraVerticalGrid.VGridControlBase.ActivateEditor(RowValueInfo cell) 
at DevExpress.XtraVerticalGrid.NormalState.MouseDown(MouseEventArgs e) 
at DevExpress.XtraVerticalGrid.BaseHandler.MouseDown(MouseEventArgs e) 
at DevExpress.XtraVerticalGrid.VGridControlBase.OnMouseDown(MouseEventArgs e) 
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks) 
at System.Windows.Forms.Control.WndProc(Message& m) 
at DevExpress.XtraEditors.Container.EditorContainer.WndProc(Message& m) 
at DevExpress.XtraVerticalGrid.VGridControlBase.WndProc(Message& m) 
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
at XXX.Program.Main(String[] args) 

所以,即使我有大量的數據在剪貼板,它應該最終完成。我等了幾分鐘...

現在它再次運作,但我仍然想知道發生了什麼!

+0

* 「當關注對象的變化」 * - 您可以使用[剪貼板事件](http://stackoverflow.com/q/621577/1997232)來代替。在指責剪貼板之前,您可以創建並向我們展示[mcve](https://stackoverflow.com/help/mcve)嗎?最好不使用DevExpress(參見[this](https://www.devexpress.com/Support/Center/Question/Details/T447517))。 – Sinatr

+0

@Sinatr我再也不能自己複製它了。我很幸運地拿到調用堆棧(serveral的時間,有時發生在backgroundworker_completed,有時它發生以後) – toATwork

回答

0

有一個在WinForms的版本,我已經看到了這個原因的剪貼板類的錯誤。在我的情況下,使用WPF版本的Clipboard類(具有相同的API)解決了這個問題。

https://msdn.microsoft.com/en-us/library/system.windows.clipboard(v=vs.110).aspx

+0

都稱之爲「OleGetClipBoard」上的「OLE32.DLL」,但System.Windows版本有附加屬性「SecurityCritical ,SuppressUnmanagedCodeSecurity「。我將檢查這些屬性的含義。 – toATwork