Category Archives: .NET

Parallel Task with ASP.NET HttpContext

When using .NET 4.0 Parallel Programming (Check my article) with ASP.NET, you will lose HttpContext once you jump in to another task.
In my case I wanted to get hold of the context from which I wanted to get the current page handler. So I came up with the below code.HOWEVER, and as you can see this is BIG HOWEVER, although I tried the code and it did work I am not 100% sure about it because the idea of manually setting the context (as you will see in a moment) and messing up with the way threading works gives me the creeps! But anyway I thought I would share it: Continue reading

A Common Question: Concurrency vs Parallelism

While presenting a .NET 4.0 session in GDC, i came across Parallel Computing. I allocated about 15 minutes to this topic since the session was a lap around .NET 4.0 features…
One of the questions I received and seemed to cause much of the confusion was the traditional one: What is the difference between Concurrency and Parallelism.

Well both concepts are related but not the same: In its simplest definition, Concurrency is about executing multiple un-related tasks in a concurrent mode. What these un-related tasks share are system resources, but they do not share a common problem to solve; meaning, they are logically independent. Now think of these tasks as threads. And since these threads share system resources, then you face the globally common problems of concurrent programming such as deadlocks and data races. This makes concurrent programming extremely difficult and debugging even more tendous.

So what is Parallelim? Parallelism is taking a certain task and dividing it into a set of realted tasks to be executed concurrently. Sweet! So again thinking of these tasks as threads, Parallel Programming still has the same problems of concurrent programming (deadlocks, data races, etc..) and introduces new challenges most notibly sharing and partitioning data across these related threads. This makes Parallel Programming even more difficult and debugging even more tendous 😦

However, it is not all bad news. .NET 4.0 parallel programming is a great step onwards. The new API solves a lot of problems (but not all) of Parallel Programming and greatly eases up Parallel Debugging…

I will post more about this great feature, but for now I hope the idea is clarified.

.NET 4.0 Code Contracts

One of the great features of .NET 4.0 is Code Contracts. Code Contracts aid in the unit testing process of code and make catching bugs before deploying the code much easier.

This post shows a sneak preview of Code Contracts in .NET Beta1 and VS 2010 Beta1. Note that by the time of the current writing, you need to install an Add-On to visual studio 2010 in order to work with code contracts. Continue reading

C# 4.0 Dynamic

one of the core functionalities of .NET 4.0 is its support for Dynamic languages. this is done primarly through the Dynamic Language Runtime (DLR) on top of the CLR. this post shows some of the usages of dynamic in C# 4.0.

consider the following class:
public class Sample
{
public static int Add(int x, int y)
{
return x + y;
}
Continue reading

Cool C# Code

i was reviewing a piece of code of a colleague, and i came across a very familiar pattern of code. basically he had a list of employees where he wanted to “loop” over and exclude any employees with age over 30. so his first attempt was the most common one: he used a foreach loop to remove all employees with age > 30. the code looked like the following

foreach (Employee empl in lst)
{
if (empl.age > 30)
lst.Remove(empl);
}
Continue reading