Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if MVC AsyncController Thread is execution in ASP.NET Pool or I/O Completion Port

I have an ASP.NET MVC 3 application. I'm using AsyncController and creating new threads. Is there a way to validate that I'm using I/O completion ports instead of ASP.NET Thread Pool?

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

Here a example of the code that I'm executing

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", Color = "White", Year = 2010},
                    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;
    }
}
like image 441
Mike Barlow - BarDev Avatar asked Apr 11 '11 04:04

Mike Barlow - BarDev


1 Answers

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

Not without seeing your code as it will depend on the APIs you are using.

For example HttpWebRequest.BeginGetResponse uses I/O Completion Ports. On the other hand if you have some CPU intensive task that you are running on a separate thread that you've created manually then you are not using I/O Completion Ports.

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

A Completion Port means that there is no thread because if there was a thread its as if you were running on a thread. Basically the way I/O Completion Ports work is that you start some I/O operation and a CP is registered with it, then you free all threads and return. During the execution of the operation there are no threads in your application associated to it. Once the operation completes the port is signaled and a thread is cretad/drawn from the pool to end the request.

like image 199
Darin Dimitrov Avatar answered Sep 30 '22 14:09

Darin Dimitrov