HostingEnvironment.QueueBackgroundWorkItem使用ASP.Net线程池还是另一个线程池? [英] HostingEnvironment.QueueBackgroundWorkItem uses ASP.Net thread pool or another thread pool?

查看:157
本文介绍了HostingEnvironment.QueueBackgroundWorkItem使用ASP.Net线程池还是另一个线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net应用程序,其中使用 HostingEnvironment.QueueBackgroundWorkItem 运行后台任务,如下代码所示.

I have an ASP.Net app, in which a background task runs using HostingEnvironment.QueueBackgroundWorkItem as in code below.

问题:下面的代码中安排的后台任务是使用ASP.Net线程池线程中的线程还是使用单独线程池中的线程?

Question: Will the background task scheduled in code below, use a thread from the ASP.Net thread pool thread, or will it use a thread from a separate thread pool?

public ActionResult SendCustMails()
{
      HostingEnvironment.QueueBackgroundWorkItem(ct => SendCustMailsTo(ct, "Customer Notification"));
      return View();
}

private void SendCustMailsTo (CancellationToken ct, string msg)
{
      //some code is omitted
      foreach (var customer in Customers)
      {
            if (ct.IsCancellationRequested)
            {
                  break;
            }

            SendMail(customer, msg);
      }

      return ct;
}

推荐答案

托管环境源代码 QueueBackgroundWorkItem方法使用_backGroundWorkScheduler字段(它是BackgroundWorkScheduler的类型)来调度后台工作项:

As can be seen from Hosting environment source code QueueBackgroundWorkItem method uses _backGroundWorkScheduler field which is type of BackgroundWorkScheduler to schedule background work items:

public sealed class HostingEnvironment : MarshalByRefObject {
//other field declarations
private static HostingEnvironment _theHostingEnvironment;
private BackgroundWorkScheduler _backgroundWorkScheduler = null; // created on demand
//yet more field declarations


//methods
 [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
    public static void QueueBackgroundWorkItem(Action<CancellationToken> workItem) {
        if (workItem == null) {
            throw new ArgumentNullException("workItem");
        }

        QueueBackgroundWorkItem(ct => { workItem(ct); return _completedTask; });
    }

    // See documentation on the other overload for a general API overview.
    //
    // This overload of QueueBackgroundWorkItem takes a Task-returning callback; the
    // work item will be considered finished when the returned Task transitions to a
    // terminal state.
    [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
    public static void QueueBackgroundWorkItem(Func<CancellationToken, Task> workItem) {
        if (workItem == null) {
            throw new ArgumentNullException("workItem");
        }
        if (_theHostingEnvironment == null) {
            throw new InvalidOperationException(); // can only be called within an ASP.NET AppDomain
        }

        _theHostingEnvironment.QueueBackgroundWorkItemInternal(workItem);
    }

    private void QueueBackgroundWorkItemInternal(Func<CancellationToken, Task> workItem) {
        Debug.Assert(workItem != null);

        BackgroundWorkScheduler scheduler = Volatile.Read(ref _backgroundWorkScheduler);

        // If the scheduler doesn't exist, lazily create it, but only allow one instance to ever be published to the backing field
        if (scheduler == null) {
            BackgroundWorkScheduler newlyCreatedScheduler = new BackgroundWorkScheduler(UnregisterObject, Misc.WriteUnhandledExceptionToEventLog);
            scheduler = Interlocked.CompareExchange(ref _backgroundWorkScheduler, newlyCreatedScheduler, null) ?? newlyCreatedScheduler;
            if (scheduler == newlyCreatedScheduler) {
                RegisterObject(scheduler); // Only call RegisterObject if we just created the "winning" one
            }
        }

        scheduler.ScheduleWorkItem(workItem);
    }
//yet more methods

}

如果我们查看 BackgroundWorkScheduler 类的源代码:

And if we look to the BackgroundWorkScheduler class's source code:

internal sealed class BackgroundWorkScheduler : IRegisteredObject {
//....
public void ScheduleWorkItem(Func<CancellationToken, Task> workItem) {
        Debug.Assert(workItem != null);

        if (_cancellationTokenHelper.IsCancellationRequested) {
            return; // we're not going to run this work item
        }

        // Unsafe* since we want to get rid of Principal and other constructs specific to the current ExecutionContext
        ThreadPool.UnsafeQueueUserWorkItem(state => {
            lock (this) {
                if (_cancellationTokenHelper.IsCancellationRequested) {
                    return; // we're not going to run this work item
                }
                else {
                    _numExecutingWorkItems++;
                }
            }

            RunWorkItemImpl((Func<CancellationToken, Task>)state);
        }, workItem);
    }
//other methods
}

我们可以注意到,它内部使用Asp.Net ThreadPool安排工作项目.

we can notice that it internally uses Asp.Net ThreadPool to schedule work items.

这篇关于HostingEnvironment.QueueBackgroundWorkItem使用ASP.Net线程池还是另一个线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆