Tuesday, November 26, 2013

Session : Databound App + Online XML Data Source using Windows Phone 8 - Windows Phone App Challenge Workshop at Microsoft Sri Lanka

Conducted a session last Saturday under the subject “Databound App + Online XML Data Source using Windows Phone 8”. The whole workshop was sponsored by Microsoft Sri Lanka. There in my session I went through MVVM for Windows Phone, Data Binding and demonstrated how you can create a simple Windows Phone 8 App which consumes data from online XML data source.


I have uploaded the slide deck and the demo project to my SkyDrive. Do check it out.


Happy Coding.

Regards,
Jaliya

Tuesday, November 19, 2013

Visual C# Technical Guru - October 2013

Became the Visual C# Technical Guru for the month of October, 2013. What else to say, feeling great.

The TechNet Guru Awards celebrate the technical articles on Microsoft TechNet.

Post in WikiNinjas Official Blog,
http://blogs.technet.com/b/wikininjas/archive/2013/11/17/technet-guru-awards-october-2013.aspx
Visual C# Technical Guru - October 2013
Visual C# Technical Guru - October 2013

Happy Coding.

Regards,
Jaliya

Saturday, November 9, 2013

Session : Generics In and Out at Sri Lanka .NET Forum

Yesterday conducted a session at Sri Lanka .NET Forum.



I have uploaded the slide deck and demo projects to my SkyDrive. Do check it out.



Happy Coding.

Regards,
Jaliya

Thursday, November 7, 2013

Data Parallelism (Task Parallel Library)

The Task Parallel Library (TPL) was introduced with .NET Framework 4 and it is the preferred way to write multi threaded and parallel code starting from .NET Framework 4. Basically TPL is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces.

TPL is mainly categorized into following three categories,
  • Data Parallelism
  • Task Parallelism
  • TPL with Asynchronous Programming Model (APM) and Event-based Asynchronous Pattern (EAP)
Since one of my favorite topics include Parallel Programming, thought to write a post about TPL even though TPL is introduced back in 2010 (if I am not mistaken). I will be writing about Data Parallelism in TPL.

What is Data Parallelism

In simple Data Parallelism is concurrently accessing and performing some heavy operation on items in a source collection or array (to be more specific here, any collection which implements Enumerable or IEnumerable<T>). The reason for me to highlight some heavy operation is sometimes parallel operations can slow down or decrease the performance. So as a best practice, do use TPL only when it is necessary, in situations like where you have hundreds of items to iterate and do some heavy processing on each of those item.

Now let’s see some code in action. For Data Parallelism we are using Parallel.For and Parallel.ForEach methods which are available in System.Threading.Tasks.Parallel class. Basically Parallel.For and Parallel.ForEach will be used to accomplish what for and foreach loops does, only difference is the iterations will be done in parallel.

For the demonstration, I will be using Parallel.ForEach on a List<T>.

I have a console application and I have the following type which is “Employee”.
public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public int CalculatedProperty { get; set; }    

    public static List<Employee> GetEmployees()
    {
        return new List<Employee>() 
        { 
            new Employee(){EmployeeId=1,FirstName="Jaliya"},
            new Employee(){EmployeeId=2,FirstName="John"},
            new Employee(){EmployeeId=3,FirstName="Jane"}
        };
    }
}
There I have a helper method which will return some data.

Now I have a method where I am passing a Employee, and the method will do some calculation and will assign the calculated result to CalculatedPropery of Employee.
static Random oRandom = new Random(); 

static void ProcessEmployee(Employee employee)
{
    // showing the current thread which the process runs in
    Console.WriteLine(string.Format("{0}'s Calculation Started on Thread {1}.", employee.FirstName, Thread.CurrentThread.ManagedThreadId));
    // to demonstrate a long operation, I am putting a thread sleep 
    Thread.Sleep(5000);
    // getting a random number to make it look nice
    employee.CalculatedProperty = oRandom.Next(0, 10000);
    Console.WriteLine(string.Format("{0}'s Calculation Completed.", 
        employee.FirstName));
}
Here I have put a Thread.Sleep for 5 seconds to demonstrate time consuming operation and setting up a random number for CalculatedPropery of Employee (just to demonstrate).

Now let’s move into the Main method.
static void Main(string[] args)
{
    // stopwatch to get the time taken to complete 
    Stopwatch watch = new Stopwatch();
    watch.Start(); 

    var employeeList = Employee.GetEmployees(); 

    // for each employee, do the processing
    foreach (var item in employeeList)
    {
        ProcessEmployee(item);
    } 

    // process completed
    // stop the watch and print the elapsed time
    watch.Stop();
    Console.WriteLine(string.Format("\nTime taken to complete = {0}s\n",watch.Elapsed.Seconds.ToString()));
 
    // just printing the result
    foreach (Employee employee in employeeList)
    {
        Console.WriteLine(string.Format("{0}'s Calculated Value {1}.",employee.FirstName,employee.CalculatedProperty));
    }
    Console.ReadLine();
}
Here first I have created a Stopwatch to get the time taken to complete. Then I am getting a List of employees and for each employee I am doing the heavy operation. After it’s completed, stopping the watch and printing the time took to complete. And finally printing the result (it’s not much important anyway).

So this is the output.

Untitled
Output : Thread.Sleep()
OK, a neat output. Started on the first employee, completed his processing, moved to the second, and so on and so forth. So if to process a single item needs 5 seconds, to process 3 items you will need 15 seconds (a simple math). Everything is happening on the same thread as usual.

But what if I process all these 3 items in parallel? Then the amount of time taken should be less right? Let’s use Data Parallelism and find out what the result is. I am modifying the above code. Instead of doing a foreach on employee list, I am going to use Parallel.ForEach.

Here is the code modified.
static void Main(string[] args){

    // stopwatch to get the time taken to complete 
    Stopwatch watch = new Stopwatch();
    watch.Start();
 
    var employeeList = Employee.GetEmployees();

    // putting a Parallel.ForEach in employee list
    Parallel.ForEach(employeeList, e => ProcessEmployee(e));
 
    // process completed
    // stop the watch and print the elapsed time
    watch.Stop();
    Console.WriteLine(string.Format("\nTime taken to complete = {0}s\n",watch.Elapsed.Seconds.ToString())); 

    // just printing the result
    foreach (Employee employee in employeeList)
    {
        Console.WriteLine(string.Format("{0}'s Calculated Value {1}.",employee.FirstName,employee.CalculatedProperty));
    }
    Console.ReadLine();
}
As you can see, I don’t have a foreach on employee list, and I do have a  Parallel.ForEach. Now let’s go ahead and run this and see the result.

Untitled1
Output : Task.Delay()
So let’s examine the printed messages from the top. First employees’ calculation is started on Thread 10. In the same time, processing of other employees has been started on different threads. They have completed processing on different order (not a problem there). And amazingly time taken has been reduced to 5 seconds. 

So there is something different from the previous run. What’s is happening here is, when I am using Parallel.ForEach, the given action is happening on separate threads providing concurrency.

Something important to note here. When I mean separate threads, in a situation like where you have thousands of items in the collection, and when you use Parallel.ForEach, the framework will not spawn thousands of records. According to MSDN,
The .NET thread pool adapts dynamically to changing workloads by allowing the number of worker threads for parallel tasks to change over time. At run time, the system observes whether increasing the number of threads improves or degrades overall throughput and adjusts the number of worker threads accordingly.
In simple, when Parallel.ForEach is partitioning the collection, whether to spawn a new thread is decided based on continually monitoring the work done by the threads allocated. If a operation is still going on and a thread is waiting around, the algorithm will spawn up more threads and re partition the collection between them. Please note that in some cases you can specify the degree of parallelism too.

For more information,
     Does Parallel.For use one Task per iteration?

So that's it. Please find the sample.

Happy Coding.

Regards,
Jaliya

Wednesday, November 6, 2013

Thread.Sleep vs. Task.Delay

We use both Thread.Sleep() and Task.Delay() to suspend the execution of a program for some given time. But are we actually suspending the execution? What is the difference between these two? How to abort from a Sleeping thread or from a delaying task. Those are some of the questions I believe most of us have. Hopefully I am trying to answer all the questions above.

Let’s go by an example. I have a very simple windows forms application which has the following form.

pic1
Form
I have the following two basic helper methods and I am calling these two methods from my “Thread Sleep” and “Task Delay” button.
void PutThreadSleep()
{
    Thread.Sleep(5000);
}
 
async Task PutTaskDelay()
{
    await Task.Delay(5000, tokenSource.Token);
} 

private void btnThreadSleep_Click(object sender, EventArgs e)
{
    PutThreadSleep();
    MessageBox.Show("I am back");
} 

private async void btnTaskDelay_Click(object sender, EventArgs e)
{
    await PutTaskDelay();
    MessageBox.Show("I am back");
}
Basically there is nothing to describe about the code up there (I am not going to explain what async and await here, you can find many articles in MSDN and one of my previous post explaining async/await). When I clicked both these buttons, the message boxes will be displayed after 5 seconds. But there are significant differences between these two ways. Let’s find out what.

Thread.Sleep()

This is the classical way of suspending a execution. This method will suspend the current thread until the elapse of giving time.When you put the Thread.Sleep in the above way, there is nothing you can do to abort this except by waiting till the time elapses or by restarting the application. That’s because this suspends the main thread the program is running. And because of that the UI is not responsive.

Task.Delay()

When comparing to Thread.Sleep(), Task.Delay() acts in a different way. Basically Task.Delay() will create a task which will complete after a time delay. This task will be running in a different thread, UI is responsive, and that's because Task.Delay() is not blocking the main thread.

Behind the scene what is happening is there is a timer ticking till the specified time. Since there is a timer, anytime we can cancel the task delay by stopping the timer. To cancel, I am modifying the above PutTaskDelay() method as follows.
CancellationTokenSource tokenSource = new CancellationTokenSource(); 

async Task PutTaskDelay()
{ 
   try
   {
       await Task.Delay(5000, tokenSource.Token);
   }
   catch (TaskCanceledException ex)
   {
       
   }
   catch (Exception ex)
   { 
   
   }
}
Here when the task got cancelled, it will be throwing me a TaskCanceledException. I am just catching the exception and suppressing it, because  don't want to show any message about that.

So in my “Cancel Task Delay” button click event, I am asking the task to be cancelled.
private void btnCancelTaskDelay_Click(object sender, EventArgs e)
{
    tokenSource.Cancel();
}
Once the task has been cancelled, the control returns immediately to the next line and message box will be shown.

So that’s it. I am uploading the sample to my SkyDrive, do check it out.


Happy Coding.

Regards,
Jaliya