Archive for June, 2011

Asynchronous Delegates Programming Sample

June 15, 2011 1 comment

In this part, we will take a look at how you could use polling to figure out if the Asynchronous call is complete. So, Let’s take a look at the code (and read the comments to get a better understanding!).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace EventAndDelegateDemo
//The delegate must have the same signature as the method. In this case,
//we will make it same as TortoiseMethod
public delegate string TortoiseCaller(int seconds, out int threadId);

public class TortoiseClass
// The method to be executed asynchronously.
public string TortoiseMethod(int seconds, out int threadId)
Console.WriteLine("The slow method... executes...on thread {0}", Thread.CurrentThread.ManagedThreadId);
for (int i = 0; i < 5; i++)
Thread.Sleep(seconds / 5 * 1000);
//Console.WriteLine("The async task is going on thread # {0}", Thread.CurrentThread.ManagedThreadId);
threadId = Thread.CurrentThread.ManagedThreadId;
return String.Format("I worked in my sleep for {0} seconds", seconds.ToString());

//Now, that we are done with the declaration part, let’s proceed to
//consume the classes and see it in action
//The algorithm would be very simple…
// 1. Call delegate’s BeginInvoke
// 2. Do some work on the main thread
// 3. Keep polling using result’s IsCompleted property
// 4. Call EndInvoke which won’t be a blocking call this time!

public class TortoiseConsumer
static void Main()
//Instantiate a new TortoiseClass
TortoiseClass tc = new TortoiseClass();
//Let's create the delegate now
TortoiseCaller caller = new TortoiseCaller(tc.TortoiseMethod);
//The asynchronous method puts the thread id here
int threadId;
//Make the async call. Notice that this thread continues to run after making this call
Console.WriteLine("Before making the Async call... Thread ID = {0}", Thread.CurrentThread.ManagedThreadId);
IAsyncResult result = caller.BeginInvoke(25, out threadId, null, null);
//After calling the method asynchronously, the main thread continues to work...
Console.WriteLine("After making the Async call... Thread ID = {0}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Start the polling...\nWaiting for the Tortoise method to complete...");
//The IAsynResult interface uses IsCompleted property which you can use to figure out if the call is complete
//Notice that this will be a blocking call until the Async call completes.
while (result.IsCompleted == false)
//Normally, EndInvoke would be a blocking call, but in this case... it won't be.
//The reason is that we now know that the Async call is completed!
string returnValue = caller.EndInvoke(out threadId, result);
Console.WriteLine("\nThe call got executed on thread {0}", threadId);
Console.WriteLine("The value returned was - {0}", returnValue);
Categories: C#

Clear cache so that cached page is not served to user (ASP.Net)

June 15, 2011 1 comment

Clear cache so that cached page is not served to user.

Response.ExpiresAbsolute=DateTime.Now.Subtract(new TimeSpan(1,0,0,0));

//end clear cache

Categories: ASP.Net

Dynamic select statement in LINQ

June 14, 2011 3 comments

how to use ‘select’ the full objects. But if you want to grab select parts, or change names of some stuff, it is not difficult. A simple dynamic Select could look like this:


use library System.Linq.Dynamic.dll

var results = db.branch .Select("new(branch_id,display)");

That would just grab the branch_id and display values of the branch items. But you can also

change the name of the column they are represented in which can often be useful in

dynamically produced tables and such:

var results = db.branch .Select("new(branch_id as BRANCH,display as Sarvesh)");

Those columns will now be titled ‘BRANCH’ and ‘Sarvesh’ respectively. Not too tough there. I

Dynamic Order By

var sortedrecord = from Filter in collection
                                   orderby OrderByDynamic(ColName, Filter) ascending
                                   select Filter;
                foreach (clsCustomer obj in sortedrecord)

object OrderByDynamic(string Key, clsCustomer By)
            object obj=new object();
            if (Key == "Name")
                obj= By.Name;
            else if (Key == "EffDate")
                obj = By.EffDate;
            return obj;
Categories: LINQ