This is the online documentation for PostSharp 5.0.
Download PDF or CHM. Go to v4.3 or v5.0

Dispatching a Method to Background

Long running processes will block the further execution of code while the system waits for them to complete. When you are building applications it's common to push long running processes to the background so that other processes can continue without waiting. Two common ways of doing this are with asyncronous processing and the BackgroundWorker. Both require a lot of boiler plate code to push execution to another thread.

PostSharp provides you with the ability to push execution of a method to a background thread without having to worry about all of the boiler plate code.

To add execute a method in the background:

  1. Add the PostSharp.Patterns.Threading package to your project.

  2. Add using PostSharp.Patterns.Threading namespace to your file.

  3. Add the BackgroundAttribute to the method that you want to push to the background for execution. The method must have void, Task or Task<TResult> return type.

Those simple steps are all that is required for you to declare that a method should be executed in a background thread.

If the method is void, it will be executed in the background, and the caller code will not wait until the background method completes its execution. If the method returns a Task, the method will be fully executed in the background (even the first segment of the method, before the first await keyword).

See Also