Saturday, 2 August 2014

Moving a window to another monitor

I had done this so many times using mouse and every time I did, I wondered why isn't there a key board short cut to do it. Dragging it felt so slow and at times it hindered productivity.

In Windows 7, the short cut key is:   

Win+Shift+left: to move the selected window to left

Win+Shift+right: to move the selected window to right

If you have only two monitors then pressing left or right again will switch the window between the left and right monitors.

I need to test this on more than two monitors. If you have 3 monitors, then let me know how it works.

Wednesday, 26 March 2014

Task Library: Common Patterns

Creating a new task

void Main()
{
    Console.WriteLine ("I'm the main thread, starting a new task. My ID: " + Thread.CurrentThread.ManagedThreadId);    
    
    //the task is started immediately
    Task t = Task.Factory.StartNew(() => 
    { 
        Console.WriteLine ("I'm a task running concurrently with the thread that started me. My ID: "
                                + Thread.CurrentThread.ManagedThreadId);
    });
    
    Console.WriteLine ("I'm going away. My ID: " + Thread.CurrentThread.ManagedThreadId);
}



I'm the main thread, starting a new task. My ID: 20
I'm going away. My ID: 20
I'm a task running concurrently with the thread that started me. My ID: 24


The method Task.Factory.StartNew() creates a task and immediately starts it. If we want to control when a task starts, we can instantiate a new Task object and call Start() on it.

For e.g.

void Main()
{
    Console.WriteLine ("I'm the main thread, starting a new task. My ID: " + Thread.CurrentThread.ManagedThreadId);
    
    //define task
    Task t = new Task(() => 
    { 
        Console.WriteLine ("I'm a task running concurrently with the thread that started me. My ID: " 
                + Thread.CurrentThread.ManagedThreadId);
    });
    
    //start the task
    t.Start();
    
    Console.WriteLine ("I'm going away. My ID: " + Thread.CurrentThread.ManagedThreadId);
}

Waiting for a task
We can see above, that the main thread doesn’t wait for the task to complete. The task may not even start when main thread is done. The main thread with ID:20 exits before the Task thread ID:24 gets to work.

The application doesn’t wait for the Task to complete because the thread which runs the Task is a background thread. To wait on a thread we use the Wait() method on the Task object.

void Main()
{
    Console.WriteLine ("I'm the main thread, starting a new task. My ID: " + Thread.CurrentThread.ManagedThreadId);
    
    Task t = Task.Factory.StartNew(() => 
    { 
        Thread.Sleep(5000);
        Console.WriteLine ("I'm a task running concurrently with the thread that started me. My ID: " 
                + Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine ("Task: I'm going away after waking up. My ID: " + Thread.CurrentThread.ManagedThreadId);
    });
    
    t.Wait();
    
    Console.WriteLine ("Main: I'm going away after waiting. My ID: " + Thread.CurrentThread.ManagedThreadId);
}




I'm the main thread, starting a new task. My ID: 22
I'm a task running concurrently with the thread that started me. My ID: 8
Task: I'm going away after waking up. My ID: 8
Main: I'm going away after waiting. My ID: 22


Passing parameter


There are overloads available in Task.Factory.StartNew() and “new Task()” constructor that take in a parameter of type object. Let’s see an example. In the previous example the Task slept for a fixed 5 seconds. Let us change it so that the Main thread can pass in the seconds to sleep.

void Main()
{
    Console.WriteLine ("I'm the main thread, starting a new task. My ID: " + Thread.CurrentThread.ManagedThreadId);
    
    Task t = Task.Factory.StartNew((sleepingTime) => //using Action<object>
    {
        Console.WriteLine ("I'm a task running concurrently with the thread that started me. My ID: " 
                + Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine ("Sleeping for " + sleepingTime);
        Thread.Sleep(Convert.ToInt32(sleepingTime));
        Console.WriteLine ("Task: I'm going away after waking up. My ID: " + Thread.CurrentThread.ManagedThreadId);
    }, 1000); //passing the parameter value
        
    t.Wait();    
    
    Console.WriteLine ("Main: I'm going away after waiting. My ID: " + Thread.CurrentThread.ManagedThreadId);        
}

We are using the overload – Task.Factory.StartNew(Action<object> action, object state). So we added a variable in the lambda and passed a value (1000) to the StartNew method.

Output:




I'm the main thread, starting a new task. My ID: 19
I'm a task running concurrently with the thread that started me. My ID: 4
Sleeping for 1000
Task: I'm going away after waking up. My ID: 4
Main: I'm going away after waiting. My ID: 19


Returning a value


This is a major difference between using ThreadPool’s QueueUserWorkItem and Task library. The QueueUserWorkItem gave back a bool value which was really of no use. But in TPL we have a Task object which represents a running operation, so we can use it to return a value as well.

We have to use the generic Task<T> when we want to create a task that returns a value. The type of return value is determined by the <T> parameter. Let’s see an example.

void Main()
{
    Console.WriteLine ("I'm the main thread, starting a new task. My ID: " + Thread.CurrentThread.ManagedThreadId);    
    //creating Task<int> which means a Task which returns an integer

    Task<int> t = Task.Factory.StartNew<int>((orderID) => 
    {
        Console.WriteLine ("Task: Calling service with input = " + orderID);
        int shippingRates = CalculateShippingRatesFromFedExWebService();
        Console.WriteLine ("Task: I'm going away. My ID: " + Thread.CurrentThread.ManagedThreadId);
        return shippingRates;        
    }, 980);
        
    int rates = t.Result;
    Console.WriteLine ("Main: Returned value from Task = " + rates);
    
    Console.WriteLine ("Main: I'm going away. My ID: " + Thread.CurrentThread.ManagedThreadId);
}

Changes:
- used the generic Task<int> in the task definition and StartNew method.
- added a return statement in the task body which returns an integer
- used Task.Result to get the return value

You may have noticed that we didn’t use “t.Wait” anymore to wait on the Task. This is because when the Result property is accessed then there is an implicit wait called on the task. If the task had been completed by the time we access the Result property, then it returns immediately otherwise it would wait until the task is finished.

Doing something after the task finishes

In the code samples above, we are running the task and then immediately waiting for it - which is not really useful. But if you think about UI applications (WPF or WinForms), then you do not need to wait on the task. The event handler or the UI command (in WPF) which starts the task can immediately return allowing the UI to be responsive and later when the task finishes it can go ahead and update the UI.

To specify a block of code that we want to run after a task finishes – or to say create a continuation task, we use “ContinueWith”.

void Main()
{
    //create task and start it immediately
    Task<int> t = Task.Factory.StartNew<int>(orderID =>
    {
        int shippingRates = CalculateShippingRatesFromFedEx(orderID);
        Console.WriteLine ("Task Thread ID:" + Thread.CurrentThread.ManagedThreadId);
        return shippingRates;
    }, 980);

    //attach the continuation block
    t.ContinueWith(antecedent =>
    {
        Console.WriteLine ("ContinueWith Thread ID:" + Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine ("Rates = " + antecedent.Result);       
    });   
    Console.WriteLine ("Main Ends");
}

Output:
Main Ends
Task Thread ID:12
ContinueWith Thread ID:12
Rates = 882

The ContinueWith method accepts an Action<Task> delegate. The parameter passed is of type Task and it references the task that ran before. Important concept is that ContinueWith returns a new Task. So using this we can create a chain of tasks each running one after another.

You may notice that both the task and its continuation task run on the same thread [ID:12]. But this is not always the case, the continuation task can be run on any thread. We can illustrate this by adding a Sleep in the Main method after creating the first task.

void Main()
{
    //create task and start it immediately
    Task<int> t = Task.Factory.StartNew<int>(orderID =>
    {
        int shippingRates = CalculateShippingRatesFromFedEx(orderID);
        Console.WriteLine ("Task Thread ID:" + Thread.CurrentThread.ManagedThreadId);
        return shippingRates;
    }, 980);

    Thread.Sleep(10);
    //attach the continuation block
    t.ContinueWith(antecedent =>
    {
        Console.WriteLine ("ContinueWith Thread ID:" + Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine ("Rates = " + antecedent.Result);       
    });   
    Console.WriteLine ("Main Ends");
}

Output:
Task Thread ID:15
Main Ends
ContinueWith Thread ID:12
Rates = 882


The only difference in previous two code snippets is the Thread.Sleep(10) introduced in the last one. Notice that the thread ID of task is now [15] and that of continuation is [12]. So it is not guaranteed, the TPL knows the best and decides which thread to use to run the continuation.

However, if you want to force that the continuation run on the same thread you can use the TaskContinuationOptions.ExecuteSynchronously when creating the continuation task. For e.g.:

//attach the continuation block
t.ContinueWith(antecedent =>
{
    Console.WriteLine ("ContinueWith Thread ID:" + Thread.CurrentThread.ManagedThreadId);
    Console.WriteLine ("Rates = " + antecedent.Result);       
},TaskContinuationOptions.ExecuteSynchronously);   


Assigning continuation task after task has completed

That is all good, but what if I create the continuation task after the antecedent task has already completed? Do I have to create the continuation task in the next line itself, after creating the antecedent task? Isn’t that brittle?

Good questions, let’s see what happens if we attach the continuation task after the task has finished.

void Main()
{
    Console.WriteLine ("Main Thread ID: " + Thread.CurrentThread.ManagedThreadId);
    //create task and start it immediately
    Task<int> t = Task.Factory.StartNew<int>(orderID =>
    {
        int shippingRates = CalculateShippingRatesFromFedEx(orderID);
        Console.WriteLine ("Task Thread ID: " + Thread.CurrentThread.ManagedThreadId);
        //Thread.Sleep(10);
        return shippingRates;
    }, 980);
    //main thread will block here for the task to finish
    Console.WriteLine ("1. Rates = " + t.Result);
    //get the task status
    Console.WriteLine ("Task Status = " + t.Status);
    //we attach the continuation block after the task is finished
    t.ContinueWith(antecedent =>
    {
        Console.WriteLine ("Continuation Task Thread ID: " + Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine ("2. Rates = " + antecedent.Result);       
    },TaskContinuationOptions.ExecuteSynchronously);   
    Console.WriteLine ("Main Ends");
}

Output:
Main Thread ID: 33
Task Thread ID: 12
1. Rates = 882
Task Status = RanToCompletion
Continuation Task Thread ID: 33
2. Rates = 882
Main Ends


In the code sample above, we attach a continuation task after the antecedent task “RanToCompletion”. Did you notice the thread ID of the continuation task in the output? It is the same as the Main thread. It means that a new Task wasn’t created to run the continuation task, the Main (or UI) thread ran it synchronously.

Coming soon...
- Updating UI from continuation task so that cross-thread access exceptions are not raised

- Exceptions in tasks






Wednesday, 29 May 2013

CruiseControl.NET: Trigger Project From its URL

Recently we had a requirement to trigger CCNet projects from a Nant file. I thought since Nant and CruiseControl work so well together it would be a piece of cake. But no, Nant doesn’t have anything to trigger projects.

The CCTray connects to CCNet server using Remoting, in days when WCF looks old there are still applications using Remoting :-). I could, in Nant, write C# code and use the library provided by CruiseControl and do exactly what CCTray does to trigger a project. Well, it’s possible, but there was still something missing.

If I go to a project’s web dashboard and click on the ‘Force Build’ button – then what happens? I thought that since the dashboard is in asp.net, it might well make a Remoting call on the server-side. But to investigate I fired up Fiddler and this is what I saw:

POST http://your-ccnet-server/ccnet/server/local/project/your-ccnet-project/ViewProjectReport.aspx HTTP/1.1
Host: your-ccnet-server
Connection: keep-alive
Content-Length: 16
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: http://your-ccnet-server
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://your-ccnet-server/ccnet/server/local/project/your-ccnet-project/ViewProjectReport.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

ForceBuild=Force

See the ‘ForceBuild=Force” at the end of the request – it was a happy moment when I saw it.

I formed this URL, browsed to it and the project happily triggered: http://your-build-server/ccnet/server/local/project/your-project-name/ViewProjectReport.aspx?ForceBuild=Force

(The "local" in the URL could vary depending on your dashboard configuration. For that, first try to fetch project report from CCTray and see what is the URL of your Cruise Control.NET project. Based on the URL, modify it to trigger the project)

Monday, 27 May 2013

TPL: Parent-Child Tasks and Captured Variables

What do you think will be the return value of the Task named “parentTask”?

void Main()
{
Console.WriteLine ("Main start.");
//captured variable
int i = 100;

Task<int> parentTask = new Task<int>(()=>
{
Console.WriteLine ("In parent start");

Task childTask1 = Task.Factory.StartNew(() => {
Thread.Sleep(1000);
Interlocked.Increment(ref i);
Console.WriteLine ("In child 1:" + i);
}, TaskCreationOptions.AttachedToParent);

Task childTask2 = Task.Factory.StartNew(() => {
Thread.Sleep(2000);
Interlocked.Increment(ref i);
Console.WriteLine ("In child 2:" + i);
}, TaskCreationOptions.AttachedToParent );

Console.WriteLine ("In parent end");
return i;
});

parentTask.Start();

Console.WriteLine ("Calling Result.");
//next statement will wait on the task
Console.WriteLine (parentTask.Result);

Console.WriteLine ("Main end.");
}

If a parent-child task hierarchy is set by using TaskCreationOptions.AttachedToParent when creating child tasks, then the parent task waits for all the child tasks to finish. But just because the parent waits for its children to finish, it doesn’t mean it would wait for them before determining its return value.

In the sample above, the line parentTask.Result is a blocking call and so it would wait for the parentTask to finish. But still the increments in the child tasks do not affect the captured variable “i”. The parentTask would need to explicitly wait for the child tasks to return the correct value.

The output of above sample is:


Main start.
Calling Result.
In parent start
In parent end
In child 1:101
In child 2:102
100
Main end.

Sunday, 5 May 2013

.NET Thread Pool

Contents

1. What’s a thread pool and why do we need it?
2. Is it per AppDomain or process?
3. What are the characteristics of a thread in the thread pool?
4. How is thread pool used by the .NET framework?
5. How can we use the thread pool?
6. QueueUserWorkItem
7. Threads are background or foreground?
8. Task Parallel Library
9. When to avoid using thread pool?
10. How many threads are available in the pool?
11. Exceptions
12. Resources


What’s a thread pool and why do we need it?
A thread pool is a store of threads. This store is created at application startup. The thread pool allows developers to focus on tasks or work items and not bother with managing threads actively. It relieves them from instantiating a new thread, starting it over and managing it. Thread pool is also used a lot internally by the framework. For e.g. the asynchronous method calls (BeginInvoke) use a thread from pool, without we knowing or worrying about it at all.
Also creating new threads is expensive, so why not reuse them? When a thread completes a work item taken from the queue, it is not immediately destroyed. It goes back to the pool and waits to pick up the next task - that thread is reused. It simplifies a lot of things.

Thread pool also attempts to create an abstraction layer in programming. Many things such as timer, web services etc. wouldn’t work without threading. For instance, someone who wants to write a basic program which uses a timer to do something every few seconds needn’t worry about managing a thread and in fact knowing the ins-out of threading to use something simple as a timer would make things difficult for the developer. And why an “attempt” to create an abstraction - because a heavy usage of things which use thread pool behind the scenes causes problems, which forces a developer to learn the intricacies of thread pool. And that’s why this post.

Is it per AppDomain or process?
It is per process. Every process created in .NET has a thread pool. And since a process can have more than one AppDomain a thread can traverse several AppDomains. A thread working in the main AppDomain could be working in a different AppDomain the next millisecond.

What are the characteristics of a thread in the thread pool?
- Background threads: I explain this in detail, few sections below.

- Priority: They start with “normal” priority.

- Naming: We cannot name these threads. Naming is useful when debugging, however we can name the thread when it starts running.

- Allocation: They could be created “on-demand” or with an interval, this depends on the allocation algorithm and it has changed over time. Until a threshold (defined by SetMinThreads) is reached, new threads are created on-demand, however after that 2 threads are created every second. This 2 threads-per-seconds was before .NET 4.0, from .NET 4.0 threads are created based on factors such as throughput.

- De-allocation: Yes threads in pool can be destroyed. I was under impression that once a thread is created in pool, it is never destroyed. After all, reusability is one of the idea behind having a pool. But it makes sense to destroy threads if they are not needed. Say if there was a burst of activity and 100 threads are created – then what should we do with them after the work is done? Every thread has 1 MB of stack, so 100 MB will go wasted.

Let’s check it in code:
ThreadPool.QueueUserWorkItem(delegate
    {
        Console.WriteLine ("ManagedThreadId = " + Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine ("IsThreadPoolThread = " + Thread.CurrentThread.IsThreadPoolThread);
        Console.WriteLine ("IsBackground = " + Thread.CurrentThread.IsBackground);
        Console.WriteLine ("Priority = " + Thread.CurrentThread.Priority);
        Console.WriteLine ("Name = " + Thread.CurrentThread.Name);
    });

ManagedThreadId = 4973
IsThreadPoolThread = True
IsBackground = True
Priority = Normal
Name = {null} 

Although we cannot set these properties before the thread starts, we could however set them when the thread starts running. For e.g.:

ThreadPool.QueueUserWorkItem(delegate
    {
        Thread.CurrentThread.IsBackground = false;
        Thread.CurrentThread.Priority = ThreadPriority.Highest;
        Thread.CurrentThread.Name = "iamapooledthread";
        //do some work here
    });

How is thread pool used by the .NET framework?
Threads from the pool are used a lot by the framework. Here are few examples:
- when we make an asynchronous call to a method using a delegate’s BeginInvoke()
- when we use Timer in our application, a thread pool is used to keep calling the callback method with an interval
- when we use Task Parallel Library (System.Threading.Tasks)
- when we use Remoting or ASP.NET or WCF: a thread from pool calls the WCF service method, it also runs the ASP.NET page

How can we use the thread pool?
To use thread from the pool we have a method QueueUserWorkItem in the ThreadPool class. I am using LINQPad, that’s why we have Dump() method which is kind of Console.WriteLine(). This method accepts a delegate called WaitCallback. This delegate can point to a method which returns void and has an argument of type object.


void Main()
{
 "In Main: Start".Dump();
 ThreadPool.QueueUserWorkItem(new WaitCallback(Work));
 "In Main: Done".Dump();
}

void Work(object input)
{
 "In Work: Start".Dump();
 Thread.Sleep(1000);
 "In Work: Done".Dump(); 
}

Output:


In Main: Start
In Main: Done
In Work: Start
In Work: Done


QueueUserWorkItem
A few details about the method.

- What does QueueUserWorkItem return? - A boolean value indicating whether the work item was successfully queued or not.

- How can I pass data to the thread method? - There is an overload of QueueUserWorkItem which accepts a parameter of type ‘object’. This is the reason our ”Work” method above has a parameter “input”. In our case, since we do not use this overload, the input value is null. Overloaded example:

void Main()
{
 "In Main: Start".Dump();
 ThreadPool.QueueUserWorkItem(new WaitCallback(Work), "FromPool");
 "In Main: Done".Dump();
}

void Work(object input)
{
 "In Run: Start".Dump();
 Thread.Sleep(500);
 input.Dump();
 "In Run: Done".Dump(); 
}

Output:


In Main: Start
In Main: Done
In Run: Start
FromPool
In Run: Done

Threads are background or foreground?

In .NET, threads can be foreground or background. An essential difference is that an active foreground thread will keep the application running until it returns. And if an application is shutting down and there are only background threads running, the application will still shut and the threads taken down.

Threads created manually in code - for e.g. Thread wk = new Thread(Worker); – are foreground threads. We could change it however:

void Main()
{
 "In Main: Start".Dump();
 Thread wk = new Thread(Work);
 string s = "Thread is background?: " + wk.IsBackground;
 s.Dump();
 wk.IsBackground = true;
 wk.Start();
 "In Main: Done".Dump();

}

void Work(object input)
{
 "In Run: Start".Dump();
 Thread.Sleep(500);
 "In Run: Done".Dump(); 
}

Output:


In Main: Start
Thread is background?: False
In Main: Done
In Run: Start
In Run: Done

In the code above, after changing the thread to a background one, the Main method does not wait for the thread.

Threads in the thread pool are background threads and we cannot change them to foreground in the method which creates them . For this reason, the threads from pool are also taken down when the process terminates. We need to use some other mechanism to wait for the thread to finish.

A simple way could be to use a WaitHandle.


AutoResetEvent wh = new AutoResetEvent(false);

void Main()
{
 "In Main: Start".Dump();
 ThreadPool.QueueUserWorkItem(new WaitCallback(Work), "FromPool"); 
 wh.WaitOne();
 "In Main: Done".Dump();
}

void Work(object input)
{
 "In Run: Start".Dump();
 Thread.Sleep(500);
 "In Run: Done".Dump();
 wh.Set();
}

Output:


In Main: Start
In Run: Start
In Run: Done
In Main: Done

Now, we cannot change them to foreground in the method where QueueUserWorkItem is called, but they could be changed in the method the thread calls. For e.g. in the code given below the application would exit without waiting for the pooled thread to complete because it is background.

        static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                Console.WriteLine("In Pooled thread: Start");
                for (int i = 0; i < 1000; i++)
                {
                    Console.Write("A");
                }
                Console.WriteLine("\nIn Pooled thread: Done");
            });

            Thread.Sleep(5);
        }

Output:

image

Let’s add a new line to change it to foreground.

     static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                Thread.CurrentThread.IsBackground = false;// NEW LINE
                Console.WriteLine("In Pooled thread: Start");
                for (int i = 0; i < 1000; i++)
                {
                    Console.Write("A");
                }
                Console.WriteLine("\nIn Pooled thread: Done");
            });

            Thread.Sleep(5);
        }

Output:

image

NOTE: Please do note that we have to use the line “Thread.Sleep(5)” in the main method, otherwise the thread may never get a chance to run and before it sets itself to foreground, the application may have exited.

Task Parallel Library

.NET 4.0 came up with Task Parallel Library. Using tasks is better way of queuing items to the thread pool than using QueueUserWorkItem.

As we can see from the samples above we had to do something extra to wait for the thread to complete its task. Also there is nothing we could do to cancel the thread. Task library helps us with these requirements. It has features like – waiting and cancellation. And the code is also easier to write.

void Main()
{
    Console.WriteLine ("In Main: Starting");
    Task task = new Task(delegate 
    { 
        Console.WriteLine ("Created using task library.");
        Thread.Sleep(1000);
    });
    task.Start();
    task.Wait();
    Console.WriteLine ("In Main: Exiting");
}


When to avoid using thread pool?
- Longer duration of work: Thread pool is to be used only when the work is of short duration. The faster the thread completes its work and goes back to the pool, the better performance. If threads are assigned to work for something very long, after some time starvation could occur. New threads are not created in the pool after a limit is reached, and if all threads are busy – the work may not be performed at all.

- Priority, Naming, IsBackground: Values such as priority and background cannot be set in the QueueUserWorkItem call.

- Synchronization: If we rely on synchronization to start, wait and stop the work done by the thread, then it is very likely that the task duration will increase and could lead to starvation very soon. If we have to use WaitHandles, locks or other synchronization techniques - then creating our own threads would be a better option.

- Active thread management: Threads created manually by writing – Thread worker = new Thread(…) – have lot of things to offer. We can suspend, resume, interrupt, abort, wait on them, pass them around, add them to a list. And certain scenarios like producer-consumer queues work best when creating our own.

We do need to remember that the .NET framework is also a customer of the thread pool. The best scenario for using thread pool is fire and forget.

How many threads are available in the pool?

This is little bit tricky. There are 3 sets of numbers that we get from the thread pool.
-> Minimum, Maximum and Available.

- Minimum doesn’t mean that, that many threads will be created when the application starts. It means that threads will be created on demand until this limit is reached. And after that, new threads will be created based on allocation algorithm. In .NET 2.0 after the minimum limit is reached, a new thread is added after an interval of 500 ms.
- Maximum stays true to its word. It is the maximum no. of threads that can be created in the pool. If there are more requests, they will be kept on hold until a thread finishes its work and comes back to the pool.
- Available = Maximum – Active no. of threads

The numbers for minimum and maximum depends on external factors. Till .NET 3.5 this was on the no. of CPU core in the machine, from .NET 4.0 it is based on the virtual address space. Here are the nos. on my machine.

.NET 4.0:

image

.NET 3.5:

image

Exceptions

Any exception that occurs in a pooled thread needs to be handled, otherwise it will bring down the application. Also the exception handling code needs to be in the worker method, not where the work is queued to be taken up by the thread.
        static void Main(string[] args)
        {            
            ThreadPool.QueueUserWorkItem(Work);
            Console.ReadKey();
        }

        static void Work(object input)
        {
            try
            {
                throw new Exception("Error!");
            }
            catch (Exception ex)
            {
                //if not handled here, then it will take down the entire process
                //log message - then continue or rethrow or graceful shutdown
                Console.WriteLine(ex);
            }
        }

Resources
- Erika Parsons and Eric Eilebrecht : CLR 4 - Inside the Thread Pool
- Thread Pool developer’s blog: http://blogs.msdn.com/b/ericeil/

Friday, 3 May 2013

Difference between delegate() { } and delegate { }

Many times we use delegates such as Action<T> or Func<T, TResult> and we do not want to pass in any argument. For instance, let’s take the Click event’s delegate EventHandler.

The EventHandler delegate expects two arguments – sender and event args, but there are instances when we do not use these parameters in the method. Now when we are writing anonymous method we need to specify these arguments. Code:

 button1.Click +=
delegate(object sender, EventArgs args)
{
MessageBox.Show("Got Clicked!");
};

But, we also have another option. Code:

 button1.Click +=
delegate
{
MessageBox.Show("Got Clicked!");
};

And it works! So we do not use () after the keyword delegate, and it runs perfectly.

But do note, if we are using lambda this option is not available.

 button1.Click += () => //ERROR
{
MessageBox.Show("Got Clicked!");
};

The code above would give the error: Delegate 'System.EventHandler' does not take '0' arguments. It is because using “() =>” means a method without parameters.

With lambda we have to specify (s, e) =>. Code:

 button1.Click += (s, e) =>
{
MessageBox.Show("Got Clicked!");
};

Friday, 12 April 2013

Productivity: Two command-prompt time savers

Recently while watching pluralsight.com videos I noticed that the authors use a console window which allowed them to select and copy text using mouse. And they also had auto-completion for files/directories. As a developer, we work a lot with console applications now and then and having these two features should be a huge time saver.

1> Text selection, copy and paste

Normally we have to click Edit –> Mark –>  Select text –> Press Enter, but there is a quicker way!

- Right-click the title bar and click Properties.

image

image

 

 

 


 

 

 

 


 

- In the Options tab, look for “Quick Edit Mode” and enable it.

image

 

 

 

 

 

 


 

With this setting, we can use left-button mouse to select text and then right-button click or ENTER to copy it. And to paste something, we can click right-button.

image

 

 

 

 


 

 

 




2> Auto-completion for files/directories:

For the auto-completion we need to modify the registry. The steps are:
- Open regedit.exe
- Look for HKEY_CURRENT_USER\Software\Microsoft\Command Processor
- Change the value of CompletionChar to 9.

image

 


 

 

 

 




 

On my computer this was already set to 9, it could be different on yours. Using screen shot I cannot show you how it works, but start by pressing [TAB] on console and it will iterate the directories/files one by one. And when you type in ‘CD D:\[TAB]’ then it would again do the same thing for D:\ drive.

You can also type in a letter, say ‘w’, and then press [TAB] – it will one by one go through all the items in the folder which start with ‘w’.

Hope knowing this saves a few minutes of your life.

Tuesday, 9 April 2013

C#: object, var and dynamic

One of the interesting concept introduced in C# 4 was the “dynamic” keyword. Let’s see how it is different from the “var” or our old friend “object”.

1. System.Object has been with us since the birth of .NET framework. It is the base class of almost everything. It means we can write:

1:      object ostring = "Hello";
2:      object oint = 555;
3:      oint = "Hello int"; //point to a string
4:      ostring = -100;//point to an int

Usage: It is used when the type could be anything. For example a method using object parameter to accept any type and then casting it to appropriate type or use reflection to call operations; or a collection which can hold any type. However with the advent of generics ‘object’ is used less often when creating collections. The primary goal of object class is to be a base class and offer low-level services, which include methods which are common to almost every type in .NET framework or created by users. Methods like ToString(), Equals(), GetHashCode(), MemberwiseClone() are provided in the object class which can be reused or overridden (except Finalize of course which can’t be overridden).

2. “var” was introduced in C# 3.0 mainly to support anonymous types created using LINQ.

Taking this LINQ query:

1:  var courses = from course in Courses
2:                where course.Category.Contains("C#")
3:                select new {course.Title, course.Description, course.VideoLength};

The query will return a collection of type IEnumerable. What do you think will be the type of the items in the IEnumerable returned?

If we run this line:


courses.First().GetType().ToString();
//above line returns: <>f__AnonymousType0`3[System.String,System.String,System.String]

we get a type name which is generated dynamically!

But we may ask why not just use ‘object’ type to write it this way:

1:  IEnumerable<object> courses = from course in Courses
2:                where course.Category.Contains("C#")
3:                select new {course.Title, course.Description, course.VideoLength};
4:   
5:  foreach (var course in courses)
6:  {
7:      course.Dump();
8:  }

Yes it compiles without error but you will face a problem when you iterate the collection’s items. Let me attach a screen shot to show what I mean:

image

Since we specified <object> in the IEnumerable we get types as objects. But if we use var – it would implicitly create a new type and expose its properties to you. Again a screen shot with ‘var’

image

That’s the power of anonymous types, you don’t need to declare a new class with the three properties which you want from the collection. Let the compiler do everything for you!

Usage: ‘var’ cannot be used if they are not initialized and they cannot be used in method parameters.

   1:  void Main()
   2:  {
   3:      var i; //Error: Implictly-typed local variables must be initialized
   4:  }
   5:   
   6:  void Test(var i) //Error: The contextual keyword 'var' may only appear within a local variable declaration
   7:  {
   8:      
   9:  }

Also once the var is declared with a specific type it cannot be assigned a value which is not compatible. For e.g.

1:      var i = 123;
2:      i = false; //Error: Cannot implictly convert type'bool' to 'int'

3. “dynamic”: Dynamic was introduced in C# 4.0. It is very different from ‘var’ which is evaluated at compile time, where the dynamic variable is determined at run-time. Based on the value assigned to the dynamic variable the appropriate type is determined at run time (also known as late-binding).

1:      dynamic d = "Hello";
2:      string type = d.GetType().ToString(); //System.String
3:   
4:      d = false;
5:      type = d.GetType().ToString(); //System.Boolean

The above code compiles successfully. In fact even this will compile:

1:      dynamic dmic = 555;
2:      dmic.DoSomethingOnThisNumber();

But of course at run time it will identify it as System.Int and give error: “RuntimeBinderException: 'int' does not contain a definition for 'DoSomethingOnThisNumber'”Dynamic can also be used in un-initialized fields and method parameters. So both of uses below are legal:

1:  void Main()
2:  {
3:      dynamic i; //No Error
4:  }
5:   
6:  void Test(dynamic i) //No Error
7:  {
8:      
9:  }

Wednesday, 13 March 2013

WCF: Duplex Contracts

WCF supports three types of message exchange patterns:

1. Request-Reply
This is the default mode. A point to note is that even if the return type of a service operation is “void” a response would still be sent back to the client.

2. One-way
In one-way operations, the service after queuing up the call doesn’t send anything back to the client. Few details:
- we need to apply “IsOneWay=true” on the operation we want to behave like a one-way
- method type return must be void
- for any exceptions in the service operation, client doesn’t receive any feedback (faults are not returned back).
- the client after making the call returns back immediately to continue execution even if the service operation is still in progress.

3. Duplex
Duplex allows two-way communication. The client makes a call to the service, and then later the service can make a separate call to the client (this call is different from the reply sent back to client for the first call). The call the service makes to the client has its own request-response flow. In Duplex communication, we need to have two contracts, one implemented by the service (incoming contract) and one by the client (callback contract).

Classic examples of duplex communication are chat applications, stock quote or game score notifications. Let’s try a stock quotes notification service.

Not all channels support duplex communication. The supported ones are: NetTcpBinding, NetNamedPipeBinding. and on HTTP we need to use the special wsDualHttpBinding (the standard http bindings - basicHttpBinding and wsHttpBinding are not bi-directional)

Demo - Stock Quote Service Application: Assume we are a service provider of stock quotes. We want to expose a service which would allow clients first to register with a stock symbol to receive stock quotes, and as long as client doesn’t disconnect or explicitly unregisters we will keep sending back messages with stock quotes.

Contracts:

We need two contracts in duplex:
- IRequestStockQuotes: incoming contract implemented at the service end
- IQuoteNotification: callback contract implemented by the client

namespace StockQuotes.Common
{
[ServiceContract(CallbackContract=typeof(IQuoteNotification))]
public interface IRequestStockQuotes
{
[OperationContract]
void RequestNotifications(string symbol);
}

[ServiceContract]
public interface IQuoteNotification
{
[OperationContract]
void SendQuote(StockQuote quote);
}
}

Service :

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class StockService : IRequestStockQuotes
{
public void RequestNotifications(string symbol)
{
Console.WriteLine("Got request for symbol:" + symbol);
IQuoteNotification client = OperationContext.Current.GetCallbackChannel<IQuoteNotification>();
StockQuote quote = QuoteEngine.GetQuote(symbol); //from a real-time quote server
client.SendQuote(quote);
}
}

The concurrency mode of the service needs to be ConcurrencyMode.Multiple or Reentrant, the default mode ConcurrencyMode.Single would create a deadlock situation. An alternative could be mark the callback operations “One-Way”.. In the service method we fetch the client callback from the OperationContext.Current.GetCallbackChannel<T> and call the method “SendQuote” to call the client.

Service Host:

ServiceHost host = null;
try
{
host = new ServiceHost(typeof(StockQuotesService.StockService));
host.Open();
foreach (ServiceEndpoint ep in host.Description.Endpoints)
{
Console.WriteLine(ep.Address.Uri.ToString());
}
Console.WriteLine("Stock service now running. \n<ENTER> to close.");
Console.ReadLine();
host.Close();
}
catch (CommunicationException)
{
if (host != null)
{
host.Abort();
}
}
catch (TimeoutException)
{
if (host != null)
{
host.Abort();
}
}
catch (Exception)
{
if (host != null)
{
host.Abort();
}
}

Client:

First we implement the callback contract -

namespace Client
{
public class QuoteNotification : IRequestStockQuotesCallback
{
public void SendQuote(StockQuote quote)
{
Console.WriteLine("Received quote (callback) from service: Symbol {0}, Quote {1}", quote.Symbol, quote.Quote);
}
}
}

An important point to note here is that , I used ‘Add Service Reference’ from Visual Studio and that’s why the name of callback contract was changed from IQuoteNotification to IRequestStockQuotesCallback. I think this was done by the code-generation utility to match the service interface name “IRequestStockQuotes”.

The calls:

static void Main(string[] args)
{
InstanceContext ctx = new InstanceContext(new QuoteNotification());
RequestStockQuotesClient client = null;

try
{
client = new RequestStockQuotesClient(ctx);
client.Open();
Console.WriteLine("ENTER to call service:");
Console.ReadLine();
client.RequestNotifications("MSFT");
client.RequestNotifications("INTL");
client.RequestNotifications("RIM");
client.RequestNotifications("NSDQ");
client.RequestNotifications("A");
Console.ReadLine();
client.Close();
}
catch (CommunicationException)
{
if (client != null)
{
client.Abort();
}
}
catch (TimeoutException)
{
if (client != null)
{
client.Abort();
}
}
catch (Exception)
{
if (client != null)
{
client.Abort();
}
}
}

Output:

Service console:


image
Client console:

image 
Further improvements:

What we did was our first implementation, there is a scope of improvement here. Right now the client calls the “RequestNotifications” service method and immediately the service sends back the quote. To get the next quote the client will again have to call the service, which is not a good implementation. Ideally the client should register to receive the quote stream and then whenever service gets a new quote it should send it to the client. This would create the live-streaming environment. We will try to accomplish that in next post.

Saturday, 9 March 2013

Introducing OData

Last weekend I introduced myself to OData. Here are the findings :-)

Contents:

1. Scenarios
2. Approaches
3. OData
4. Using LINQPad to access NetFlix
5. Examples from the Real World (ecosystem)
6. Resources
7. Developing OData Services(WCF Data Service, ASP.NET Web API)
8. Accessing OData Services (.NET client, jQuery)
9. Limitations
10. And GData?

First let’s start with the scenarios or the domain where OData can be relevant.

Scenarios:

- Let’s say you are a company which runs buses. You are looking for ways to share the information about buses such as arrival time, frequency, operating hours, capacity, service closures etc. You want other web sites and smart phone applications to be able to access this data. How would you do it?
- Say you are working for an online video streaming company like NetFlix and you want to expose your data about movie titles, availability, reviews etc. You want to share this information over the web so that e-commerce web sites like Amazon can create a catalog of your products or you are writing a Windows 8 app to display data from NetFlix.
- Say you are manager of a government department and you have a legal requirement to expose the data about projects such as date of completion, cost, overruns, tenders etc. on a web site and also in a format which can be easily accessed by other govt. applications

Now for all these scenarios we could have created a web site and displayed data on it, thereby creating a data island. But how can we make the data “reusable”, so that other applications (web /mobile) could consume it?

Also, a common requirement among the use cases above is that they return a list of entries or items. It is not about a request-response where you build a request package with some parameters and the service sends back the response after doing some calculation. This is more about - I want to expose data on my company’s URL say www.example.com/movies to share all the movie titles or www.busservice.com/buses to expose information about all the buses I have and step deeper www.nybusservice.com/buses(11)/frequency to get the frequency of bus with ID of “11”.

Approaches:

1) SOAP: We could create a WCF service (which uses SOAP format), host it in IIS so that it could become part of the organization’s web infrastructure. Now SOAP enables us to use WS-* protocols, metadata (WSDL); we can define operations like GetMoviesTitles, GetMovieReviews and also give us security, transactions, reliability, interoperability over HTTP. It supports request-response pattern over HTTP Post. But that is a lot of stuff made available to us, which may not be useful to use in certain scenarios.

2) POX (Plain Old XML): We could expose a ASP.NET URL and return XML data (could be done by writing our own http handler). The problem here is the data structure. The clients will have to ‘know’ the schema before hand to access the data and also we need to write extra stuff to enable CRUD operations. The advantage here is we avoid all the SOAP overhead.

3) REST: REST is an architectural style using the concept of ‘resources’ and how we can identify them with a URI, along with a fixed verbs for actions – GET, POST, PUT and DELETE for the CRUD operations on those resources. By using these HTTP verbs, it is easy for consumers to realize what actions need to be performed for interacting with the data.

In the scenarios above, let’s add one more - assume you run a blogging engine like Blogger, WordPress etc. The posts that your users write on your blogging engine needs to be exposed in a way so that blog readers (like Google Reader) could read the posts. You could again send back XML containing a list of all the posts in your own XML format. But then what would happen if every blog engine out there created its own format. Of course as we know this problem has been solved – all the blog engines use a standard format called Atom or RSS. This makes it easier for feed readers to just point to a URL and get all the posts and because they know it’s in Atom format it makes it quite easy for them. The Atom format is only for getting resources, and AtomPub (used by OData) is for editing web resources.

If we come to think of it, wouldn’t it better if we could return an Atom/RSS feed from our services? The atom/rss feed XML is understood by modern day browsers so anyone with a browser could go to the URL and get the data. This is in fact possible today with WCF. We could create RESTful services and reply back syndication feeds in RSS or ATOM format, sending back a list of your business objects.

But there is still a need of some more functionality to make it useful. Functionality like metadata, paging and filtering the feeds, for this we use OData. So we can say “OData is exposing your business objects (as a list/collection) over HTTP URL in a RESTful manner, by returning data in XML-AtomPub format along with providing facilities like metadata, filtering and paging.”

OData:

The “O” in OData stands for “Open”, the full name is “Open Data Protocol”. It is REST based, using HTTP + AtomPub + JSON.

The definition from OData.org:
"The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores."

Let’s explore the NetFlix OData a little. If we browse to the URL http://odata.netflix.com/v2/Catalog/, this is what we get:

image

Under the <service> we have various collections: Genres, Titles, TitleAwards so on. If want to get all the titles we browse to: http://odata.netflix.com/v2/Catalog/Titles, and we get

image


We can see here it starts with the tag <feed> and there is an <entry> tag for every Title returned by the NetFlix service. In the <entry> you will find an “id” which will point to the URL we can use to get details about that particular Title.

image 
If we go to URL http://odata.netflix.com/v2/Catalog/Titles('13aly') we get:

image

Using LINQPad to access OData services:

In the previous section we saw how we can see the OData in browser, now we will see how we can use LINQPad to access it.

I am using LINQPad 4, but the steps should be same for other versions. The first step is to ‘Add connection’ in LINQPad, for this you can click the ‘Add Connection’ link given in the left pane.

image 
Upon clicking we get this dialog:

image 
Select “WCF Data Services 5.1..” and click Next and enter the URI.

image 
After clicking OK, you should get the NetFlix in the connection list along with various collections it exposes.

image 
We can now query the data with LINQ queries. 

> Get 10 genres:
Genres.Take(10)

image 
> Get the movies released this year: 

image 
Examples (Live):

- http://pluralsight.com/odata/ (PluralSight is my favorite web site, here it gives data about courses, authors, modules etc.)
- http://data.stackexchange.com/stackoverflow/atom (the stackoverflow web site gives a good amount of data for questions, answers, comments etc.)
- http://odata.netflix.com/v2/Catalog/
- and more here: http://www.odata.org/ecosystem. This link gives the list of many companies which are offering OData as of today.

Resources:

- OData.org: Introduction
- MSDN: WCF Data Services
- MSDN: Open Data Protocol by Example
- OData with Scott Hanselman
- MSDN: Getting Started With OData Part 1: Building an OData Service
- MSDN: Getting Started With OData Part 2: Building an OData Services from Any Data Source
- OData: There’s is a feed for that

(The rest of sections will be coming in the next blog post)