1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Runtime.InteropServices;
6
using System.Windows.Forms;
8
namespace ICSharpCode.Core.WinForms
11
/// Helper class to access the clipboard without worrying about ExternalExceptions
13
public static class ClipboardWrapper
15
[Obsolete("Avoid using this property: it is problematic because it requires exclusive clipboard access. " +
16
"The Clipboard.ContainsText() implementation in WPF is much better than the one in WinForms.")]
17
public static bool ContainsText {
20
return Clipboard.ContainsText();
21
} catch (ExternalException) {
27
public static string GetText()
29
// retry 2 times should be enough for read access
31
return Clipboard.GetText();
32
} catch (ExternalException) {
33
return Clipboard.GetText();
37
public static void SetText(string text)
39
DataObject data = new DataObject();
40
data.SetData(DataFormats.UnicodeText, true, text);
45
/// Gets the current clipboard content.
48
public static IDataObject GetDataObject()
50
// retry 2 times should be enough for read access
52
return Clipboard.GetDataObject();
53
} catch (ExternalException) {
55
return Clipboard.GetDataObject();
56
} catch (ExternalException) {
62
public static void SetDataObject(object data)
64
SafeSetClipboard(data);
67
// Code duplication: TextAreaClipboardHandler.cs also has SafeSetClipboard
68
[ThreadStatic] static int SafeSetClipboardDataVersion;
70
static void SafeSetClipboard(object dataObject)
72
// Work around ExternalException bug. (SD2-426)
73
// Best reproducable inside Virtual PC.
74
int version = unchecked(++SafeSetClipboardDataVersion);
76
Clipboard.SetDataObject(dataObject, true);
77
} catch (ExternalException) {
78
Timer timer = new Timer();
80
timer.Tick += delegate {
83
if (SafeSetClipboardDataVersion == version) {
85
Clipboard.SetDataObject(dataObject, true, 10, 50);
86
} catch (ExternalException) { }