0

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);
    }
}
Guido
  • 926
  • 2
  • 10
  • 19
  • _so that it is not locked anymore by the app_ - What does this mean? – Igor Mar 13 '13 at 09:13
  • You might want to add that you got the classes you use from [here](http://stackoverflow.com/questions/899350/how-to-copy-the-contents-of-a-string-to-the-clipboard-in-c) because they are not part of the .NET Framework. – nvoigt Mar 13 '13 at 09:18
  • I'm assuming that an application is locking the usage of the clipboard ressource for a certain amount of time before another application can use it. – Guido Mar 13 '13 at 09:19
  • @nvoigt that is correct. I got already a lot of help from the stackoverflow community. It is the best place for coding people :-) – Guido Mar 13 '13 at 09:21

1 Answers1

0

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.

Igor
  • 1,532
  • 4
  • 23
  • 44