Difference between Task.Run and QueueBackgroundWorkItem in Asp.Net

26,925

Solution 1

The documentation has an excellent explanation:

Differs from a normal ThreadPool work item in that ASP.NET can keep 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. This API cannot be called outside of an ASP.NET-managed AppDomain. The provided CancellationToken will be signaled when the application is shutting down.

Task.Factory.StartNew does not register work with the ASP.NET runtime at all. You're running your code for 10 minutes, that makes no difference. IIS recycle happens at particular times which are preset in IIS. If you really want to test whats going on, you can attempt to force a recycle.

Solution 2

Below article explains something similar to what you are doing and if you go to the final section "Few more thoughts..." you see the difference highlighted between the two

http://codingcanvas.com/using-hostingenvironment-queuebackgroundworkitem-to-run-background-tasks-in-asp-net/

Basically it says that using queuebackgroundworkitem tasks are registered with ASP.Net runtime and if a process is closed or crashes ASP.NET runtime still gives some grace period for processes to complete.It also involves sending notification to the process so that it can wrap up and perform any finalization tasks whereas all this is not available when you use Task.Run

Solution 3

The AppDomain shutdown can only be delayed 90 seconds (It’s actually the minimum of HttpRuntimeSection.ShutdownTimeout and processModel shutdownTimeLimit). If you have so many items queued that they can’t be completed in 90 seconds, the ASP.NET runtime will unload the AppDomain without waiting for the work items to finish.

https://blogs.msdn.microsoft.com/webdev/2014/06/04/queuebackgroundworkitem-to-reliably-schedule-and-run-background-processes-in-asp-net/

Share:
26,925

Related videos on Youtube

Faisal Mq
Author by

Faisal Mq

• Programming fanatic with more than 10 years of experience developing Web applications in Asp.Net MVC, C#.Net, Javascript, npm, jQuery, ReactJS, CSS, SASS, J2EE etc. My passion is to stay hungry and foolish like Steve told.

Updated on July 09, 2022

Comments

  • Faisal Mq
    Faisal Mq almost 2 years

    What exactly is the difference using

    Task.Run(() => { 
         LongRunningMethod();
    });
    

    or

    HostingEnvironment.QueueBackgroundWorkItem(clt => LongRunningMethod());
    

    I tested on an Asp.Net MVC application in which I kept on writing a line to a text file for about 10 minutes inside an asynchronous task which is invoked using Task.Run or QBWI.

    It goes fine both using Task and QBWI. My async method keeps on writing to that file without any issues till 10 minutes. No disturbance from IIS I observed regarding its recycling.

    So what is special about QueueBackgroundWorkItem then?

  • Faisal Mq
    Faisal Mq almost 9 years
    Yes, I forced a recycle from IIS inetmgr both using Task and QBWI and both stopped at once.
  • Piotr Kula
    Piotr Kula over 8 years
    Forcing a recycle isnt the same as a a timed recycle. Forcing the receycle clear everything out, regardless of any tasks, because its manual and by doing so you are trying to "reset" everything. I am sure the new QueuedBackgroundWorker wont get recycled with a normal IIS timed recycle, or it delays the recycle because IIS knows something is still going on.
  • Motomotes
    Motomotes over 4 years
    I think the only real difference is QBW will get a small extension window and will receive earlier warning via the cancellation token.