views:

28

answers:

2

Is it possible to create a custom attribute , when applied to a method will execute the method in a seperate thread .

Class A {

  [threaded]
  public void execute()
  {

  }

}

main()

{ A obja = new A(); obja.execute() // will excute in a new thread if [threaded] attribute is applied }

A: 

Quick answer: No.

If you create an attribute, the runtime does not know what to do with it (how could it?). Your code could check and then execute in the threadpool (or in .NET 4, via a Task), but you will have to ensure you always check.

The .NET runtime (CLR) knows about the attributes it knows about, and can make execution decisions at runtime. (As can the compiler, e.g. the compiler knows about ConditionalAttribute to allow conditional compilation.)

Richard
A: 

You can use PostSharp to achieve this.

Yurec