Long-running processes will block the further execution of code while the system waits for them to complete. When you are building applications it is 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 asynchronous processing and the BackgroundWorker. Both require a lot of boilerplate 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 boilerplate code.
Executing a method in the background
Add the
PostSharp.Patterns.Threadingpackage to your project.Import the
PostSharp.Patterns.Threadingnamespace into your file.Add the BackgroundAttribute to the method that you want to push to the background for execution. The return type of the method must be
void, Task or Task<TResult>.
These 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).
Methods annotated with this attribute are run in the managed thread pool, unless you use the attribute's IsLongRunning property, in which case it is run as a new separate background thread.
See Also
Reference
Other Resources