Make sure thread that runs the code is marked with [STAThread] attribute. For WinForm and console based apps it is generally Main
method
Put [STAThread]
above your main method:
[STAThread]
static void Main()
{
}
For WinForms it is usually in generated Main.cs file that you can edit if necessary (it will not be re-generated on changes). For console it's were you define the Main
.
If you can't control the thread (i.e. you are writing a library or main app is locked by some reason) you can instead run code that accesses clipboard on specially configured thread (.SetApartmentState(ApartmentState.STA)
) as shown in another answer.
Clipboard.SetText()
) from a background thread? Can you give us some more context (i.e., the code surrounding that function call)? – Cody Gray♦ Jul 20 '13 at 13:03