Below is the code how I put a text string into the Windows Clipboard. I am looking for a command to close the clipboard resource immediately after so that it is not locked anymore by the app. Do you know how to close the clipboard ressource explicitely in C#?
This is the code
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
// ????
This is the code of class 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);
}
}
If you "lock" (prevent other apps to modify) the clipboard by calling OpenClipboard() method, then you can (and you should, because it prevents other applications from using it) close it with CloseClipboard().
Here's a sequence you should stick to when copying to the clipboard.