I am switching from Task.Run to Hangfire . In .NET 4.5+, Task.Run can return a Task<TResult> , which allows me to run tasks that return except void . Usually I can wait and get the result of my task by accessing the MyReturnedTask.Result property
An example of my old code:
public void MyMainCode() { List<string> listStr = new List<string>(); listStr.Add("Bob"); listStr.Add("Kate"); listStr.Add("Yaz"); List<Task<string>> listTasks = new List<Task<string>>(); foreach(string str in listStr) { Task<string> returnedTask = Task.Run(() => GetMyString(str)); listTasks.Add(returnedTask); } foreach(Task<string> task in listTasks) {
As far as I can see on the Quick Start page Hangfire, your main guy is BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget")); perfectly executes the code as a background task, but apparently does not support tasks that have a return value (for example, the code presented above). It is right? if not, how can I configure my code to use Hangfire?
PS I already looked at HostingEnvironment.QueueBackgroundWorkItem ( here ), but apparently it lacks the same functionality (background jobs should be void )
EDIT
As @Dejan explained, the main reason I want to switch to Hangfire is the same reason that .NET people added QueueBackgroundWorkItem in .NET 4.5.2. And that reason is well described in an article by Scott Hanselman about basic tasks in ASP.NET. Therefore, I want to quote an article:
QBWI (QueueBackgroundWorkItem) schedules a task that can run in the background, regardless of any request. This differs from a regular ThreadPool work item in that ASP.NET automatically keeps track of the number of registered items through this API that are currently running, and ASP.NET runtime try to delay closing the AppDomain until these run work items are completed.
yazanpro
source share