diff --git a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs index e945e04796..ea7b82bdc4 100644 --- a/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs +++ b/src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextAreaClipboardHandler.cs @@ -87,10 +87,27 @@ namespace ICSharpCode.TextEditor dataObject.SetData(DataFormats.Rtf, RtfWriter.GenerateRtf(textArea)); } OnCopyText(new CopyTextEventArgs(str)); - Clipboard.SetDataObject(dataObject, true, 50, 50); - return true; + + // Work around ExternalException bug. (SD2-426) + // Best reproducable inside Virtual PC. + // SetDataObject has "retry" parameters, but apparently a call to "DoEvents" + // is necessary for the workaround to work. + int i = 0; + while (true) { + try { + Clipboard.SetDataObject(dataObject, true, 5, 50); + return true; + } catch (ExternalException) { + if (i++ > 5) + throw; + } + System.Threading.Thread.Sleep(50); + Application.DoEvents(); + System.Threading.Thread.Sleep(50); + } + } else { + return false; } - return false; } public void Cut(object sender, EventArgs e) diff --git a/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs b/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs index 8f49232277..4c06870d22 100644 --- a/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs +++ b/src/Main/Core/Project/Src/Util/ClipboardWrapper.cs @@ -19,6 +19,7 @@ namespace ICSharpCode.Core public static bool ContainsText { get { try { + LoggingService.Debug("ContainsText called"); return Clipboard.ContainsText(); } catch (ExternalException) { return false; @@ -55,7 +56,19 @@ namespace ICSharpCode.Core public static void SetDataObject(object data) { - Clipboard.SetDataObject(data, true, 50, 50); + int i = 0; + while (true) { + try { + Clipboard.SetDataObject(data, true, 5, 50); + return; + } catch (ExternalException) { + if (i++ > 5) + throw; + } + System.Threading.Thread.Sleep(50); + Application.DoEvents(); + System.Threading.Thread.Sleep(50); + } } } }