Asynchronously run a method with the Async and Await keywords

I wrote this small class to illustrate the use of the Async and Await keywords which were added in .NET 4.5.
It should quickly help anyone curious to understand this easier way to write asynchronous programs in C#.


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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Synchronous method");
            Console.WriteLine("Before calling DisplaySum");
            DisplaySum();
            Console.WriteLine("After calling DisplaySum");
            Console.WriteLine("Press a key to continue.");
            Console.ReadKey();

            Console.WriteLine("************************");
            Console.WriteLine("Asynchronous method");

            Console.WriteLine("Before calling DisplaySumAsync");
            DisplaySumAsync();
            Console.WriteLine("After calling DisplaySumAsync");
            Console.WriteLine("Press a key to quit.");
            Console.ReadKey();
        }

        public static double Calculate()
        {
            Console.WriteLine("Method Calculate()");
            double x = 1;
            // Long-running method
            for (int i = 1; i < 100000000; i++)
            {
                x += Math.Tan(x) / i;
            }
            return x;
        }

        // Synchronous method
        private static void DisplaySum()
        {
            Console.WriteLine("Method DisplaySum()");
            double result = Calculate();
            Console.WriteLine("DisplaySum DisplaySum - result : " + result);
        }

        // ***************************************

        public static Task<double> CalculateAsync()
        {
            Console.WriteLine("Method CalculateAsync()");
            return Task.Run(() =>
            {
                double x = 1;
                // Long-running method
                for (int i = 1; i < 100000000; i++)
                {
                    x += Math.Tan(x) / i;
                }
                return x;
            });
        }

        // Asynchronous method
        private static async void DisplaySumAsync()
        {
            Console.WriteLine("Method DisplaySumAsync()");
            double result = await CalculateAsync();
            Console.WriteLine("Method DisplaySumAsync - result : " + result);
        }
    }
}

The output below (DOS window) shows clearly the difference between the call of a synchronous method and the call of an asynchronous method.
The message “After calling DisplaySumAsync” (line 25) is called immediately after the call of the DisplaySumAsync() method, whether it is finished or not.
The important thing to note is : the lines written in the same thread used to call the asynchronous method (which runs in another thread) are executed immediately. An asynchronous method does not block the calling function, allowing that function to continue functioning.

AsyncAwait

About some of the new features of Java 8 …

Just want to signal something that a lot of Java developers seem not to be aware of :
a few of the upcoming features of the JDK 8 have been available in C# for some time now.

.NET / C# Java
Lambda expressions (closures) .NET 3.5 - C# 3 - 2007 JDK 8 - 2013
Virtual extension methods .NET 3.5 - C# 3 - 2007 JDK 8 - 2013
... ... ...

C# was inspired by Java and C++ and now it kind of feels like Java 8 is catching up with C# !
But it is a good thing when the languages influence each other.
That is the case with C# and Java but that is also the case with Windows Forms / WPF + XAML and JavaFX 2 / GWT or ASP.NET MVC and Java MVC-based frameworks, etc.

That is another good reason to be open to both worlds.

How to use customized entities

CRM Dynamics contains native entities (contact, account, opportunity, appointment …).
The CRM SDK API already contains specific classes for these native entities.
It is possible to create customized entities. To use them in programs, i found that there
are two options for that : either use crm webservice or use the CRM SDK API. With the latter option, you have to use Dynamic entity to access your custom entities.
Continue reading

Steps to run a plug-in for the Dynamics CRM in debug mode

Here are the steps to run a plug-in in debug mode
1) In Visual Studio (BIDS), build the project
2) Copy the generated DLL file and the PDB file (they are located in the project folder, under bin\Debug) to the CRM assembly folder : C:\Program Files\Microsoft Dynamics CRM\Server\bin\assembly
3) Deploy or redeploy the plug-in using the Plug-in registration tool
Continue reading