Created
October 22, 2012 06:27
-
-
Save esergueev/3929975 to your computer and use it in GitHub Desktop.
ASP.NET MVC background worker for long running tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IBackgroundWorker | |
| { | |
| Task<TOutput> Run<TInput, TOutput>(Func<TInput, TOutput> action); | |
| Task Run(Action action); | |
| Task Run<TInput>(Action<TInput> action); | |
| } | |
| public sealed class BackgroundWorker : IBackgroundWorker | |
| { | |
| public Task<TOutput> Run<TInput, TOutput>(Func<TInput, TOutput> action) | |
| { | |
| return Task<TOutput>.Factory.StartNew(() => | |
| { | |
| var service = DependencyResolver.Current.GetService<TInput>(); | |
| return action(service); | |
| }, TaskCreationOptions.LongRunning); | |
| } | |
| public Task Run(Action action) | |
| { | |
| return Task.Factory.StartNew(action, TaskCreationOptions.LongRunning); | |
| } | |
| public Task Run<TInput>(Action<TInput> action) | |
| { | |
| return Task.Factory.StartNew(() => | |
| { | |
| var service = DependencyResolver.Current.GetService<TInput>(); | |
| action(service); | |
| }, TaskCreationOptions.LongRunning); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment