asp.net-mvc – 如何判断MVC AsyncController线程是否在ASP.NET池或I / O完成端口中执行

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 如何判断MVC AsyncController线程是否在ASP.NET池或I / O完成端口中执行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个ASP.NET MVC 3应用程序.我正在使用AsyncController并创建新线程.有没有办法验证我使用的是I / O完成端口而不是ASP.NET线程池?

Thread.CurrentThread或其他地方是否有属性我可以检查以确定线程的运行位置?

这是我正在执行的代码示例

public class HomeController : AsyncController
{
    public void CarsComplexAsync(string make)
    {
        AsyncManager.OutstandingOperations.Increment(2);

        System.Diagnostics.Debug.WriteLine("Enter CarsComplexAsync: " + DateTime.Now);

        Action getCarsAsync = () =>
        {
            List<Car> cars = CarService.GetCars(make);
            AsyncManager.Parameters["cars"] = cars;
            AsyncManager.OutstandingOperations.Decrement();    
        };


        Action getTrucksAsync = () =>
        {
            List<Car> trucks = CarService.GetTrucks(make);
            AsyncManager.Parameters["trucks"] = trucks;
            AsyncManager.OutstandingOperations.Decrement();
        };


        getCarsAsync.BeginInvoke(null,null);
        getTrucksAsync.BeginInvoke(null,null);
    }
public ActionResult CarsComplexCompleted(List<Car> cars,List<Car> trucks)
{
cars.AddRange(trucks);
return View(cars);
}
public static class CarService
{
    private static List<Car> _cars = new List<Car>
                {
                    new Car{ Make = "Ford",Model = "F-150",Color = "White",Year = 2010},new Car{ Make = "Chevy",Model = "Camero",Color = "Black",Year = 1984},new Car{ Make = "Peugeot",Model = "406 Coupe",new Car{ Make = "Dodge",Model = "Charger",Color = "Red",Year = 1974}
                };

    public static List<Car> GetCars(string model)
    {
        Thread.Sleep(5000);
        List<Car> cars = _cars;
        return cars;
    }

    public  static List<Car> GetTrucks(string make)
    {
        Thread.Sleep(5000);
        List<Car> cars = _cars;
        return cars;
    }
}

解决方法

Is there a way to validate that I’m using I/O completion ports instead of ASP.NET Thread Pool?

不是没有看到你的代码,因为它取决于你正在使用的API.

例如,HttpWebRequest.BeginGetResponse使用I / O完成端口.另一方面,如果您有一些cpu密集型任务,您正在手动创建的单独线程上运行,那么您不使用I / O完成端口.

Is there a property in Thread.CurrentThread or someplace else that I can check to identify where the thread is running?

完成端口意味着没有线程,因为如果有一个线程,就好像你在一个线程上运行一样.基本上I / O完成端口的工作方式是你启动一些I / O操作并注册CP,然后释放所有线程并返回.在执行操作期间,应用程序中没有与之关联的线程.一旦操作完成,就会发信号通知端口,并从池中删除/绘制一个线程以结束请求.

猜你在找的asp.Net相关文章