PostSharpAPI ReferencePost­Sharp.​Patterns.​ThreadingBackground­Attribute
Open sandboxFocus

BackgroundAttribute Class

Custom attribute that, when applied on a method, causes the method to execute in the background. This aspect can be applied to a method with void or Task return type.

Namespace: PostSharp.Patterns.Threading
Assembly: PostSharp.Patterns.Threading.dll
[MethodInterceptionAspectConfiguration(SerializerType = null)]
[Metric("UsedFeatures", "PostSharp.Patterns.Threading.BackgroundMethod")]
[AspectDescription("Executes the method asynchronously in the thread pool")]
public sealed class BackgroundAttribute : MethodInterceptionAspect, IMethodLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IAsyncMethodInterceptionAspect, IMethodInterceptionAspect, IMethodLevelAspect, IAspect

When this aspect is applied to a method returning a Task, the aspects wraps the method call by a call to Task.Factory.StartNew, therefore executing the whole method in a background thread. Without this aspect, the target method would execute asynchronously, but in the current context, instead of in the background context.

If the IsLongRunning property is set to true, the method will be executed in a new thread. Otherwise, it will be queued into the ThreadPool.

If the target method results in an exception, the exception can be handled by subscribing to the UnhandledException event. Unhandled exceptions are rethrown, typically causing the application to crash.


Name Description

Initializes a new BackgroundAttribute.


Name Description

Specifies that the method is long-running and should be executed on its own thread, if that is supported on the target platform. If false, the method will be executed in the thread pool.


Name Description

Method invoked at build time to ensure that the aspect has been applied to the right target.


Method invoked instead of the method to which the aspect has been applied.


Method invoked instead of the method to which the aspect has been applied.