For developers the quality of a program can be often express in the time used to finish the computation. With current multicore processor we have to move our thinking from a serial execution to a concurrent execution. Using delegates in an asynchronous way can force the CLR to allocate multiple threads to your computation.

In below code sample, you will see that I call one method multiple times. This method simply waits 5 seconds. Calling 6 times this method in a synchronous way makes the time to finish obvious: at least 30 seconds! Now using the BeginInvoke method from my delegate I can add multiple threads to these computations. On my machine the time to achieve this is now 9 seconds only!

using System;
using System.Diagnostics;
using System.Text;
using System.Threading;

 

namespace Threads

{

    class Program

    {

        public delegate string myDelegate(string txt);

 

        static void Main(string[] args)

        {

            SynchronousDelegateSample();

            AsynchronousDelegateSample();

 

            Console.WriteLine(“Main thread exits.”);

            Console.ReadKey();

        }

 

        static void SynchronousDelegateSample()

        {

            Stopwatch sw = new Stopwatch();

            sw.Start();

 

            /*

             * All the calls to the delegate will be synchronous

             * This mean that the order will always be 1,2,3,4,5,6

             * The thread used will always be the same.

             */           

            myDelegate dm = new myDelegate(DelegateMethod);

            Console.WriteLine(dm(“calling delegate (1)”));

            Console.WriteLine(dm(“calling delegate (2)”));

            Console.WriteLine(dm(“calling delegate (3)”));

            Console.WriteLine(dm(“calling delegate (4)”));

            Console.WriteLine(dm(“calling delegate (5)”));

            Console.WriteLine(dm(“calling delegate (6)”));

 

            sw.Stop();

            Console.WriteLine(“All work was done in: {0} milliseconds.\n\n”, sw.ElapsedMilliseconds);

        }

 

        static void AsynchronousDelegateSample()

        {

            Stopwatch sw = new Stopwatch();

            sw.Start();

 

            /*

             * All the work will be invoked in a blink.

             * Multiple thread will be used to compute the result of the method.

             */

            myDelegate dm = new myDelegate(DelegateMethod);

            IAsyncResult result1 = dm.BeginInvoke(“calling delegate (1)”, null, null);

            IAsyncResult result2 = dm.BeginInvoke(“calling delegate (2)”, null, null);

            IAsyncResult result3 = dm.BeginInvoke(“calling delegate (3)”, null, null);

            IAsyncResult result4 = dm.BeginInvoke(“calling delegate (4)”, null, null);

            IAsyncResult result5 = dm.BeginInvoke(“calling delegate (5)”, null, null);

            IAsyncResult result6 = dm.BeginInvoke(“calling delegate (6)”, null, null);

 

            /*

             * EndInvoke is synchronous we force to wait for the asynchronous results.

             */

            string r1 = dm.EndInvoke(result1);

            string r2 = dm.EndInvoke(result2);

            string r3 = dm.EndInvoke(result3);

            string r4 = dm.EndInvoke(result4);

            string r5 = dm.EndInvoke(result5);

            string r6 = dm.EndInvoke(result6);

 

            sw.Stop();

            Console.WriteLine(“All asynchronous  work was done in: {0}”,

                sw.ElapsedMilliseconds);

        }              

 

        /// <summary>

        /// Method called by our delegate

        /// Will wait for a random time (between 1 and 10000 milliseconds).

        /// </summary>

        /// <param name=”txt”>Any string are okay.</param>

        /// <returns>A string confirming the end of the process.</returns>

        static string DelegateMethod(string txt)

        {

            // Wait 5 seconds.

            Thread.Sleep(5000);

 

            // Work is finished

            Console.WriteLine(txt + ” \n> Thread ID is: {0}”,

                Thread.CurrentThread.ManagedThreadId.ToString());

            return “>> “ + txt + ” Done !”;

        }

    }

}

Delegate can also be used to call method from different classes. I let you check at the documentation on Msdn.

 

 

This is my first post of, I hope, a series related to WPF and C#.

The examples below are made with VS (Visual Studio) 2010 and .Net 4.0, for this precise example you can make it work with .Net 3.5 at least.

During the life of an application we can listen for some important events, these events can help us to drive the logic of our application from the startup to the end of an application life.

If you are creating a simple WPF Application in VS you will get a file called App.xaml for free. In this file you can add arguments to handle events.

<Application x:Class=”WpfAppStartup.App”

             xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

             xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

             Startup=”Application_Startup”

             Exit=”Application_Exit”

             Activated=”Application_Activated”

             Deactivated=”Application_Deactivated”

             SessionEnding=”Application_SessionEnding”

             StartupUri=”MainWindow.xaml”>

 

As you can see in the above code, I added the Startup and Exit arguments in the Application node.
(Tip: just write ‘Startup=’ and then press Tab on your keyboard to have the method name and signature automatically created in App.xaml.cs file).

Below is the list of Events you can catch and a short description.

Name

Description

Startup

Called when Application.Run() is called, shortly before the main window is displayed. This is where you can extract all cmdline arguments.

Exit

Called when the application is shut down, shortly before the Run() methods returns.

Activated

Called when application get focus.

Deactivated

Called when application lose focus.

SessionEnding

Occurs when the Windows session is ending (log off or shut down computer).

DispatcherUnhandledException

Called when an Unhandled application occurs in your application (main thread).

 

Now back to our first goal: getting the arguments array!

private void Application_Startup(object sender, StartupEventArgs e)

{

if (e.Args.Length > 0)

       {

             foreach (string arg in e.Args)

              {

                    // logic for arguments processing.

              }

}

       else

       {

             // No arguments!

}

 

MessageBox.Show(“Application.Run() is called, “ +

“this happen before the main window is displayed.”);

}


If there are any unclear sentence, please feel free to comment!

 

Search engine are not doing search anymore… No search is for small player, now the real thing is to have an evolved engine. Where Wolframalpha offer a computational knowledge engine, Bing offer a decision engine. So far Google can keep being Google fearless ; )

Bing (also known as Kumo) should be publicly available on June 3rd. At least the name is easy to remember :)

You can already get some information on it:
- Behind bing
- Decision engine

I personally think it is a good idea to not compete with Google directly on traditional search, they real do a great job there. Live.com showed to be quite good but did not had any motivational factor to change people habits. Bing, might succeed there, I guess most of the people will be positively surprised by something else than search : )

Enjoy soon,
Ahmet

 

Just in case you haven’t seen it yet, a biographical look at the men who founded Apple and Microsoft and a look at the early days of the companies.

Enjoy,
Ahmet

 

Just noticed that Visual Studio 2010 pro (beta 1) is now available to download as a web installer.

I am personnaly quite new to the VS world, I have been using it daily for only 9 month now. I must say that I am still quite impresse by the simplicity and the power this tool have.

Just try it, it is free so you can make up your own opinion and tell me what you think of it :)

For more info just head to the VS 2010 beta website.

Enjoy,
Ahmet

 

While trying to download Windows7 today (like a lot of other people it seems as the server are too busy), I saw this postpone message from the Windows blog, I share it here as it is so slow to access:

Due to very heavy traffic we’re seeing as a result of interest in the Windows 7 Beta, we are adding some additional infrastructure support to the Microsoft.com properties before we post the public beta. We want to ensure customers have the best possible experience when downloading the beta, and I’ll be posting here again soon once the beta goes live. Stay tuned! We are excited that you are excited!

The good news is that the link has been made available via neowin.

And the build exist as a torrent from some days already.

Now the bad news:

There is no key so far, for them we have to wait tommorow where *only* 2.5 millions key will be distributed :o

Any way, the key can be entered later I guess.
Enjoy the test !

A.

 

I’m a bit late for propagating the news, so you may have already heard that Microsoft has opened the Zurich Development Center (December 2008).

 Microsoft Zurich Development Center

 

We are working on bits for Office Communication Server.

The work enviromement is really great, we got very confortable offices with a fitness room and a game room (ping-pong, dart, table soccer, shuffle puck, pool) and an enormous cafetaria : )

 

Ahmet