From cea87a1bccd743e48895ba9e1c6bd9181122972e Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Thu, 8 Mar 2007 18:56:10 +0000 Subject: [PATCH] Fixed SD2-1329: Index was out of range when cutting text. Replace DoEvents() with a timer to make waiting for the second clipboard set operation non-blocking. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2432 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/Gui/TextAreaClipboardHandler.cs | 37 ++++++++++++++----- .../Core/Project/Src/Util/ClipboardWrapper.cs | 29 ++++++++++++--- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs index a50343b228..8cec01ec0a 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs @@ -88,21 +88,38 @@ namespace ICSharpCode.TextEditor } OnCopyText(new CopyTextEventArgs(stringToCopy)); - // Work around ExternalException bug. (SD2-426) - // Best reproducable inside Virtual PC. - try { - Clipboard.SetDataObject(dataObject, true, 10, 50); - } catch (ExternalException) { - Application.DoEvents(); - try { - Clipboard.SetDataObject(dataObject, true, 10, 50); - } catch (ExternalException) {} - } + SafeSetClipboard(dataObject); return true; } else { return false; } } + + // Code duplication: TextAreaClipboardHandler.cs also has SafeSetClipboard + [ThreadStatic] static int SafeSetClipboardDataVersion; + + static void SafeSetClipboard(object dataObject) + { + // Work around ExternalException bug. (SD2-426) + // Best reproducable inside Virtual PC. + int version = unchecked(++SafeSetClipboardDataVersion); + try { + Clipboard.SetDataObject(dataObject, true); + } catch (ExternalException) { + Timer timer = new Timer(); + timer.Interval = 100; + timer.Tick += delegate { + timer.Stop(); + timer.Dispose(); + if (SafeSetClipboardDataVersion == version) { + try { + Clipboard.SetDataObject(dataObject, true, 10, 50); + } catch (ExternalException) { } + } + }; + timer.Start(); + } + } bool CopyTextToClipboard(string stringToCopy) { diff --git a/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs b/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs index f7c5d1422b..3d7d2acbc9 100644 --- a/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs +++ b/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs @@ -60,13 +60,32 @@ namespace ICSharpCode.Core public static void SetDataObject(object data) { + SafeSetClipboard(data); + } + + // Code duplication: TextAreaClipboardHandler.cs also has SafeSetClipboard + [ThreadStatic] static int SafeSetClipboardDataVersion; + + static void SafeSetClipboard(object dataObject) + { + // Work around ExternalException bug. (SD2-426) + // Best reproducable inside Virtual PC. + int version = unchecked(++SafeSetClipboardDataVersion); try { - Clipboard.SetDataObject(data, true, 10, 50); + Clipboard.SetDataObject(dataObject, true); } catch (ExternalException) { - Application.DoEvents(); - try { - Clipboard.SetDataObject(data, true, 10, 50); - } catch (ExternalException) { } + Timer timer = new Timer(); + timer.Interval = 100; + timer.Tick += delegate { + timer.Stop(); + timer.Dispose(); + if (SafeSetClipboardDataVersion == version) { + try { + Clipboard.SetDataObject(dataObject, true, 10, 50); + } catch (ExternalException) { } + } + }; + timer.Start(); } } }