Use [STAThread] in a console program in C#

user2442074 picture user2442074 · Jun 27, 2013 · Viewed 10.8k times · Source

I have written code for a C# console application. It copies a clipboard value to a file, and it runs without any error.

Now I want to use it in another C# project with other code.

I use [STAThread] after class{}, but it give me an error:

:: Attribute 'STAThread' is not valid on this declaration type. It is only valid on 'method' declarations.

What can I do?

Answer

Dave Hogan picture Dave Hogan · Jun 27, 2013

You can only put [STAThread] above the entry point method (not class) and only once in a library. What is your project type that you're trying to copy this code to? Does it even require the STA attribute? Is the method static?

The error is saying you're putting [STAThread] on something other than an method.

Post up your code.