6

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Attributes are placed *before* the item they modify, not *after*. So, if previously you've placed `[STAThread]` *after* `class`, that has just (by coincidence) worked by appearing in the right place in the file such that the first declared *member* of the class was the thing that should have been modified. – Damien_The_Unbeliever Jun 27 '13 at 10:52
  • 1
    It is much stricter than that, [STAThread] will only ever have an affect when it is placed on the Main() entrypoint of a program. Creating another thread and using Thread.SetApartmentState() before you start it is technically possible. – Hans Passant Jun 27 '13 at 11:48

1 Answers1

12

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.

Dave Hogan
  • 3,201
  • 6
  • 29
  • 54