Difference between Task.Run and QueueBackgroundWorkItem in Asp.Net

后端 未结 3 1747
你的背包
你的背包 2020-12-13 17:32

What exactly is the difference using

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

or

HostingEnvironment.QueueBack         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 18:21

    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.

提交回复
热议问题