ASP.NET and Concurrency
Paul Roberts wrote a nice article [1] about using the CCR (Concurrency and Coordination Runtime) together with ASP.NET, to start multiple task concurrently. The CCR will handle the thread pool for us.
The CCR is now still a part of Robotics Studio, but there are some rumors this will be part of the .Net Framework. After implementing some helper CCR functions in our base class, we can start using the async model by spawning some tasks:
protected void Page_Load(object sender, EventArgs e)
{
…
int count = 10;
for (int i = 0; i < count; i++)
{
SpawnIterator("http://wwww.microsoft.com", resultPort, DownloadUrl);
}
….
}
IEnumerator<ITask> DownloadUrl(string url, SuccessFailurePort resultPort)
{
// Async processing to download from url.
System.Threading.Thread.Sleep(2000);
resultPort.Post(new SuccessResult());
yield break;
}
The CCR will take care of all the async responses (using Ports). So to speed this up, we can control the amount of threads in the CCR thread pool in our global.asax:
protected void Application_Start(object sender, EventArgs e)
{
Initialize(4);
}
Read the complete article for all the helper classes we need to get this working.
[1] http://blogs.msdn.com/pollrobots/archive/2008/06/09/using-ccr-with-asp-net.aspx