Open sandboxFocus
  • TOC

Post­Sharp Documentation

Introduction

Deployment and Configuration

Adding Aspects to Code

Logging

Contracts

INotify­Property­Changed

Weak Event

XAML

Parent/Child, Visitor and Disposable

Undo/Redo

Caching

Multithreading

Developing Custom Aspects

Validating Architecture

Testing and Debugging

Miscellaneous

API Reference

Post­Sharp

Post­Sharp.​Aspects

Post­Sharp.​Aspects.​Advices

Post­Sharp.​Aspects.​Configuration

Post­Sharp.​Aspects.​Dependencies

Post­Sharp.​Aspects.​Internals

Post­Sharp.​Aspects.​Serialization

Post­Sharp.​Collections

Post­Sharp.​Constraints

Post­Sharp.​Extensibility

Post­Sharp.​Extensibility.​Build­Time­Logging

Post­Sharp.​Patterns

Post­Sharp.​Patterns.​Caching

Post­Sharp.​Patterns.​Caching.​Backends

Post­Sharp.​Patterns.​Caching.​Backends.​Azure

Post­Sharp.​Patterns.​Caching.​Backends.​Redis

Post­Sharp.​Patterns.​Caching.​Dependencies

Post­Sharp.​Patterns.​Caching.​Formatters

Post­Sharp.​Patterns.​Caching.​Implementation

Post­Sharp.​Patterns.​Caching.​Locking

Post­Sharp.​Patterns.​Caching.​Serializers

Post­Sharp.​Patterns.​Caching.​Value­Adapters

Post­Sharp.​Patterns.​Collections

Post­Sharp.​Patterns.​Collections.​Advices

Post­Sharp.​Patterns.​Contracts

Post­Sharp.​Patterns.​Diagnostics

Post­Sharp.​Patterns.​Diagnostics.​Adapters.​Asp­Net­Core

Post­Sharp.​Patterns.​Diagnostics.​Adapters.​Asp­Net­Framework

Post­Sharp.​Patterns.​Diagnostics.​Adapters.​Diagnostic­Source

Post­Sharp.​Patterns.​Diagnostics.​Adapters.​Http­Client

Post­Sharp.​Patterns.​Diagnostics.​Audit

Post­Sharp.​Patterns.​Diagnostics.​Backends

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Application­Insights

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Audit

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Common­Logging

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Console

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Event­Source

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Log4Net

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Microsoft

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Multiplexer

Post­Sharp.​Patterns.​Diagnostics.​Backends.​NLog

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Null

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Serilog

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Trace

Post­Sharp.​Patterns.​Diagnostics.​Backends.​Trace­Source

Post­Sharp.​Patterns.​Diagnostics.​Contexts

Post­Sharp.​Patterns.​Diagnostics.​Correlation

Post­Sharp.​Patterns.​Diagnostics.​Custom

Post­Sharp.​Patterns.​Diagnostics.​Custom.​Messages

Post­Sharp.​Patterns.​Diagnostics.​Formatters

Post­Sharp.​Patterns.​Diagnostics.​Record­Builders

Post­Sharp.​Patterns.​Diagnostics.​Threading­Instrumentation

Post­Sharp.​Patterns.​Diagnostics.​Transactions

Post­Sharp.​Patterns.​Diagnostics.​Transactions.​Model

Post­Sharp.​Patterns.​Dynamic­Advising

Post­Sharp.​Patterns.​Formatters

Post­Sharp.​Patterns.​Model

Post­Sharp.​Patterns.​Model.​Controls

Post­Sharp.​Patterns.​Model.​Type­Adapters

Post­Sharp.​Patterns.​Recording

Post­Sharp.​Patterns.​Recording.​Operations

Post­Sharp.​Patterns.​Threading

Access­Level­Attribute3

Actor­Attribute8

Actor­Attribute.​Serializer4

Actor­Dispatcher

Ambient­Concurrency­Controller­Context

Assume­Immutable­Attribute2

Background­Attribute6

Concurrency­Controller­Factory

Concurrent­Access­Awaiter

Concurrent­Access­Exception3

Concurrent­Access­Token

Deadlock­Detected­Event­Args2

Deadlock­Detection­Policy5

Deadlock­Detection­Policy.​Serializer2

Deadlocked­Thread­Info

Deadlock­Exception3

Deadlock­Info

Dispatched­Attribute5

Dispatched­Attribute.​Serializer2

Dispatched­Execution­Mode

Dispatcher­Factory

Entry­Point­Attribute3

Explicitly­Synchronized­Attribute3

Freezable­Attribute9

Freezable­Attribute.​Serializer5

IActor

IActor­Controller

IActor­Dispatcher

IConcurrency­Controller

IDispatcher

IDispatcher­Object

IFreezable

IImmutable

Immutable­Attribute9

Immutable­Attribute.​Serializer5

IThread­Aware

Object­Read­Only­Exception2

Private­Thread­Aware­Attribute8

Private­Thread­Aware­Attribute.​Serializer4

Reader­Attribute4

Reader­Writer­Synchronized­Attribute8

Reader­Writer­Synchronized­Attribute.​Serializer4

Read­Only­Thread­Aware­Attribute8

Read­Only­Thread­Aware­Attribute.​Serializer4

Reentrant­Attribute3

Requires­Thread­Safe­Attribute5

Requires­Thread­Safe­Attribute.​Serializer2

Synchronized­Attribute8

Synchronized­Attribute.​Serializer4

Thread­Access­Exception3

Thread­Affine­Attribute8

Thread­Affine­Attribute.​Serializer4

Thread­Aware­Attribute7

Thread­Aware­Attribute.​Serializer3

Thread­Aware­Extensions

Threading­Exception2

Threading­Model

Threading­Patterns­Aspect­Roles

Threading­Services

Threading­Unhandled­Exception­Event­Args2

Threading­Unhandled­Exception­Source

Thread­Mismatch­Exception3

Thread­Safety­Policy5

Thread­Unsafe­Attribute8

Thread­Unsafe­Attribute.​Serializer4

Upgradeable­Reader­Attribute4

Writer­Attribute4

Yielder­Attribute4

Post­Sharp.​Patterns.​Threading.​Models

Post­Sharp.​Patterns.​Utilities

Post­Sharp.​Patterns.​Xaml

Post­Sharp.​Reflection

Post­Sharp.​Reflection.​Method­Body

Post­Sharp.​Serialization

  • Article

Class BackgroundAttribute

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
Syntax
[MethodInterceptionAspectConfiguration(SerializerType = typeof(MsilAspectSerializer))]
[ProvideAspectRole("Threading")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "Tracing")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "Caching")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "CacheInvalidation")]
[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, typeof(ThreadAwareAttribute))]
[Metric("UsedFeatures", "PostSharp.Patterns.Threading.BackgroundMethod")]
[LinesOfCodeAvoided(1)]
[AspectDescription("Executes the method asynchronously in the thread pool")]
public sealed class BackgroundAttribute : MethodInterceptionAspect, IMethodLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IAsyncMethodInterceptionAspect, IMethodInterceptionAspect, IMethodLevelAspect, IAspect
Remarks

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.

Constructors

Name Description
BackgroundAttribute()

Initializes a new BackgroundAttribute.

Properties

Name Description
IsLongRunning

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.

Methods

Name Description
CompileTimeValidate(MethodBase)

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

OnInvoke(MethodInterceptionArgs)

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

OnInvokeAsync(MethodInterceptionArgs)

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

  • Navigation