C# Asp.Net中Task.Run和QueueBackgroundWorkItem的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29938522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:23:41  来源:igfitidea点击:

Difference between Task.Run and QueueBackgroundWorkItem in Asp.Net

c#.netasp.net-mvcasynchronoustask-parallel-library

提问by Faisal Mq

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.

我在一个 Asp.Net MVC 应用程序上进行了测试,在该应用程序中,我在使用 Task.Run 或 QBWI 调用的异步任务中持续向文本文件写入一行大约 10 分钟。

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.

使用 Task 和 QBWI 都很好。我的异步方法继续写入该文件,直到 10 分钟都没有任何问题。我没有观察到 IIS 对其回收的干扰。

So what is special about QueueBackgroundWorkItem then?

那么 QueueBackgroundWorkItem 有什么特别之处呢?

采纳答案by Yuval Itzchakov

The documentationhas 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.

与普通 ThreadPool 工作项的不同之处在于,ASP.NET 可以跟踪通过此 API 注册的当前正在运行的工作项的数量,并且 ASP.NET 运行时将尝试延迟 AppDomain 关闭,直到这些工作项完成执行。不能在 ASP.NET 管理的 AppDomain 之外调用此 API。提供的 CancellationToken 将在应用程序关闭时发出信号。

Task.Factory.StartNewdoes 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.

Task.Factory.StartNew根本不向 ASP.NET 运行时注册工作。你运行你的代码 10 分钟,这没什么区别。IIS 回收发生在 IIS预设的特定时间。如果你真的想测试发生了什么,你可以尝试强制回收

回答by Jags

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/

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

基本上它说使用 queuebackgroundworkitem 任务是在 ASP.Net 运行时注册的,如果一个进程关闭或崩溃,ASP.NET 运行时仍然为进程提供一些宽限期来完成。它还涉及向进程发送通知,以便它可以结束并执行任何最终化任务,而当您使用 Task.Run 时,所有这些都不可用

回答by Bhupinderjit Dhanoa

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.

AppDomain 关闭只能延迟 90 秒(实际上是 HttpRuntimeSection.ShutdownTimeout 和 processModel shutdownTimeLimit 中的最小值)。如果排队的项目太多以至于无法在 90 秒内完成,ASP.NET 运行时将卸载 AppDomain,而无需等待工作项目完成。

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

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