Saturday, March 21, 2009

prevent Concurrent Asynchronous postback

By default, when a page makes multiple asynchronous postbacks at the same time, the postback made most recently takes precedence. For example, user clicks on a button which generates asynchronous postback. Now before the response of the asynchronous postback comes to client, user click another button which also generates asynchronous postback. In this case the response of the first button's event will be discarded by the client. So user will not find the update information as the  the first button's event.

So in this case there are two approaches to handle the situation:

1. When a asynchronous postback is in progress, user will not be able to initiate another postback.

2. if user initiates multiple asynchronous postbacks then we can queue the requests and send them one by one. But in this case timeout is a problem.


Approach 1: Prevent users to initiate multiple asynchronous postbacks:

    <script type="text/javascript">

        var Page;

        function pageLoad() {

            Page = Sys.WebForms.PageRequestManager.getInstance();



        function OnInitializeRequest(sender, args) {

            var postBackElement = args.get_postBackElement();

            if (Page.get_isInAsyncPostBack()) {

                alert('One request is already in progress.');





In the above code block, we are hooking OnInitializeRequest event on every request's initialize event. In the initialize event handler (OnInitializeRequest) we are checking if an asynchronous request is in progress. if so then canceling the current request.


Approach 2: Queue asynchronous requests

Andrew Fedrick describes in his blog how to queue asynchronous requests here.

For simplicity, my recommendation is to prevent users to initiate multiple asynchronous requests.


Some Useful Links

Tuesday, March 10, 2009

Covariance and Contravariance in C#

In C# roughly we can say that covariance means we can substitute derived type in place of base type. Contravariance means we can substitute base class in place of derived class (You are thinking it's not possible, right? We'll see how it's possible). To get a detail discussion on what covariance and contravariance are follow the great post on Eric Lippert's Blog or Visit Wikipedia

In C# covariance and contravariance are supported only for reference types. We will discuss few covariance and contravariance supports in C#. For this example just take a look at the following class hierarchy as we are going to use the class hierarchy for all the examples in this post:



Figure: Class hierarchy


1. From C# 1.0, arrays where the element type is reference type are covariant. For example the following statement in C# is ok.


Animal[] animals=new Mammal[10];

In the above code mammal can be stored in animals array as mammal is derived from Animal. But remember this is only true for reference types. Why this covariance only for reference types but not for value type? Its because for reference types the array originally keeps only pointers to the original object and base pointer can refer to derived types. In case of value type the original value is stored in array itself so the size many vary depending on the type. So covariance is not supported for array of values. For example the following statement will not compile:

long[] arr = new int[100];

2. Covariance from Method to delegates were included in C# 2.0. In the following code snippets (which is valid in C# 2.0 and later) you'll find that return type supports covariant. The original delegate has return type of Animal. But the method we have assigned (here, CopyMammal) to a variable (here, cfunc) has return type Mammal. So we can see that covariance is supported in return types.


//delegate which take no arguments but return animal

delegate Animal copy();



/// <summary>

/// method which delegate copy can accepts.

/// </summary>

/// <returns>Mammal</returns>

Mammal copyMammal()


return new Mammal();



The following statement is valid and an example of return type covariance.


//an assignment statement where covariant occurs by allowing Mammal return type in place of Animal return type

copy cfunc = copyMammal;


3. Contravariance is supported in parameters. Let's take a look at the following code snippets for understanding how contravariance works in parameters types:


//delegate which take one mammal argument and return nothing

delegate void CopyState(Mammal a);


void copyMammalState(Mammal mammal)




void copyAnimalState(Animal mammal)




void CopyGiraffeSate(Giraffe giraffe)





Now the following code will compile as Contravariance is supported here. This is contravariance since we are using Animal parameter of CopyAnimalState in place of Mammal defined in CopyState delegate.

CopyState cs1 = copyAnimalState;


But the following code will not supported as covariance is not supported in parameters.

CopyState cs2 = CopyGiraffeSate;

The above is not valid in C#. But why is not valid? Let's explain a bit. For shake of argument think that the above statement is valid. Then anybody can call the cs2 with an Tiger element as show below:


CopyState cs2 = CopyGiraffeSate;

Tiger tiger = new Tiger();



If covariance would support here then the above statement would generate an exception as cs2 can handle Giraffe but not Tiger.


C# 4.0 has extended the co and contravariance further for generic types and interfaces. Hope I'll post on it later. Some useful links on covariance and contravariance are as follows:

Monday, March 9, 2009

Tuple in C# 4.0

C# 4.0 include a new feature called Tuple. In mathematics tuple is a ordered list of specific number of values called the components of the tuple. For example a 3-tuple name may be used as: (First-Name, Middle-Name, Last-Name).

Let's take a look in the following example:

        public Tuple<int, int> GetDivAndRemainder(int i, int j)


            Tuple.Create(i/j, i%j);


        public void CallMethod()


            var tuple = GetDivAndRemainder(10,3);

            Console.WriteLine("{0} and {1}", tuple.item1, tuple.item2);



In the above example the method can return a tuple which has two integer values. So using tuple we can return multiple values. So this will help lazy programmers to write less code but do more. One great use of tuple might be returning multiple values from a method.

To get more info on Tuple visit the following links: