title: Hangfire Background Job with Return Value
body: <p>I'm switching from <a href="https://msdn.microsoft.com/en-us/library/hh195051%28v=vs.110%29.aspx" rel="noreferrer">Task.Run</a> to <a href="http://docs.hangfire.io/en/latest/quick-start.html" rel="noreferrer">Hangfire</a>. In .NET 4.5+ <code>Task.Run</code> can return <code>Task&lt;TResult&gt;</code> which allows me to run tasks that return other than <code>void</code>. I can normally wait and get the result of my task by accessing the property <code>MyReturnedTask.Result</code></p>
  
  <p>Example of my old code:</p>
  
  <pre><code>public void MyMainCode()
  {
      List&lt;string&gt; listStr = new List&lt;string&gt;();
      listStr.Add("Bob");
      listStr.Add("Kate");
      listStr.Add("Yaz");
  
      List&lt;Task&lt;string&gt;&gt; listTasks = new List&lt;Task&lt;string&gt;&gt;();
  
      foreach(string str in listStr)
      {
          Task&lt;string&gt; returnedTask = Task.Run(() =&gt; GetMyString(str));
          listTasks.Add(returnedTask);
      }
  
      foreach(Task&lt;string&gt; task in listTasks)
      {
          // using task.Result will cause the code to wait for the task if not yet finished.
          // Alternatively, you can use Task.WaitAll(listTasks.ToArray()) to wait for all tasks in the list to finish.
          MyTextBox.Text += task.Result + Environment.NewLine;
      }
  }
  private string GetMyString(string str)
  {
      // long execution in order to calculate the returned string
      return str + "_finished";
  }
  </code></pre>
  
  <p>As far as I can see from the <a href="http://docs.hangfire.io/en/latest/quick-start.html" rel="noreferrer">Quick Start</a> page of Hangfire, your main guy which is <code>BackgroundJob.Enqueue(() =&gt; Console.WriteLine("Fire-and-forget"));</code>
  perfectly runs the code as a background job but apparently doesn't support jobs that have a return value (like the code I presented above). Is that right? if not, how can I tweak my code in order to use Hangfire?</p>
  
  <p>P.S. I already looked at <code>HostingEnvironment.QueueBackgroundWorkItem</code> (<a href="http://blogs.msdn.com/b/webdev/archive/2014/06/04/queuebackgroundworkitem-to-reliably-schedule-and-run-long-background-process-in-asp-net.aspx" rel="noreferrer">here</a>) but it apparently lacks the same functionality (background jobs have to be <code>void</code>) </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As @Dejan figured out, the main reason I want to switch to Hangfire is the same reason the .NET folks added <code>QueueBackgroundWorkItem</code> in .NET 4.5.2. And that reason is well described in Scott Hanselman's great <a href="http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx" rel="noreferrer">article</a> about Background Tasks in ASP.NET. So I'm gonna quote from the article:</p>
  
  <blockquote>
    <p>QBWI (QueueBackgroundWorkItem) schedules a task which can run in the background, independent of
    any request. This differs from a normal ThreadPool work item in that
    ASP.NET automatically keeps track of how many work items registered
    through this API are currently running, and the ASP.NET runtime will
    try to delay AppDomain shutdown until these work items have finished
    executing.</p>
  </blockquote>