Custom attribute that, when applied to a method, specifies that the method should be executed in the UI thread.
Implements
Namespace: PostSharp.Patterns.Threading
Assembly: PostSharp.Patterns.Threading.dll
Syntax
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Interface)]
[ProvideAspectRole("Threading")]
[MulticastAttributeUsage(MulticastTargets.Method, TargetExternalMemberAttributes = MulticastAttributes.Internal, AllowMultiple = false)]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "Tracing")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "Caching")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "CacheInvalidation")]
[Metric("UsedFeatures", "PostSharp.Patterns.Threading.DispatchedMethod")]
[LinesOfCodeAvoided(0)]
[Serializer(typeof(DispatchedAttribute.Serializer))]
public sealed class DispatchedAttribute : MethodLevelAspect, IMethodLevelAspect, IMethodLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IAspectProvider, IAspect, IServiceRemarks
Execution of the method can be synchronous or asynchronous according to the value of the FireAndForget property.
This aspect can be applied to instance methods of UI controls (WinForms or WPF), or to any class implementing the IDispatcherObject manually.
When the class does not implement IDispatcherObject, the dispatcher is taken from the synchronization context of the thread that creates the object, typically the UI thread. Creating the object in a thread that has no synchronization context results in an InvalidOperationException. The aspect cannot be applied to static methods.
When an async method is called from another thread, the method is started in the dispatcher thread, and the returned task
completes when the method completes.
Constructors
| Name | Description |
|---|---|
| DispatchedAttribute() | Initializes a new instance of the DispatchedAttribute class in blocking mode. |
| DispatchedAttribute(DispatchedExecutionMode) | Initializes a new instance of the DispatchedAttribute class and specifies whether the execution mode should be blocking or non-blocking. |
| DispatchedAttribute(PortableFormatterConstructorContext) | |
| DispatchedAttribute(bool) | Initializes a new instance of the DispatchedAttribute class. |
Properties
| Name | Description |
|---|---|
| ExecutionMode | Gets or sets a value that specifies whether the caller continues immediately after enqueuing the call to the target method, without waiting for the method to complete. This property applies when the aspect is applied to a void non-async method. The default value is Blocking. |
| FireAndForget | Gets or sets a value indicating whether the caller continues immediately after enqueuing the call to the target method,
without waiting for the method to complete. This property applies when the aspect is applied to a void non-async method.
The default value is |
Methods
| Name | Description |
|---|---|
| CompileTimeValidate(MethodBase) | Method invoked at build time to ensure that the aspect has been applied to the right target. |