下面是我如何将文本字符串放入 Windows 剪贴板的代码。我正在寻找一个命令来立即关闭剪贴板资源,以便应用程序不再锁定它。你知道如何在 C# 中显式地关闭剪贴板资源吗?
这是代码
String s = "Hello World";
Thread staThread3 = new Thread
(
delegate()
{
try { new SetClipboardHelper(DataFormats.Text, s).Go();}
catch (Exception ex) { /* Exception Handling */ }
}
);
staThread3.SetApartmentState(ApartmentState.STA);
staThread3.Start();
staThread3.Join();
// Here I would like to close the clipboard
// ????
这是类 SetClipboardHelper 的代码
class SetClipboardHelper : StaHelper
{
readonly string _format;
readonly object _data;
public SetClipboardHelper(string format, object data)
{
_format = format;
_data = data;
}
protected override void Work()
{
var obj = new System.Windows.Forms.DataObject(
_format,
_data
);
Clipboard.SetDataObject(obj, true);
}
}