在過去的幾個小時中,我一直在跟蹤一個相當具體的錯誤,因爲另一個應用程序打開了剪貼板。本質上作爲剪貼板是一個共享資源(按"Why does my shared clipboard not work?"),並試圖執行如何處理被阻止的剪貼板和其他奇怪
Clipboard.SetText(string)
或
Clipboard.Clear().
下拋出異常:
System.Runtime.InteropServices.ExternalException: Requested Clipboard operation did not succeed. at System.Windows.Forms.Clipboard.ThrowIfFailed(Int32 hr) at System.Windows.Forms.Clipboard.SetDataObject(Object data, Boolean copy, Int32 retryTimes, Int32 retryDelay) at System.Windows.Forms.Clipboard.SetText(String text, TextDataFormat format) at System.Windows.Forms.Clipboard.SetText(String text)
我最初的解決方案是在短暫停頓後重試,直到我意識到Clipboard.SetDataObject具有字段的次數和延遲的長度,.NET的默認行爲是嘗試延時100毫秒10次。
有已被最終用戶注意到一個最後一件事,那就是儘管被拋出的異常的複製到剪貼板操作仍然有效,我一直沒能找到有關爲什麼這可以是任何進一步的信息。
我當前解決這個問題的方法就是默默地忽略異常......這真的是最好的方法嗎?
我已經閱讀了GetOpenClipboardWindow,似乎這是剪貼板訪問問題的最佳解決方案。感謝您的回覆。 – 2009-05-31 07:00:47
如何獲取阻止剪貼板的進程 - 請參閱:http://stackoverflow.com/questions/6583642/determine-which-process-is-locking-the-clipboard – toong 2011-09-13 14:23:39
只需先關閉剪貼板。看到我的答案。 – Triynko 2012-10-10 19:08:49