As a programmer there have been many times when I have found a bottle neck in an app I’ve written that comes down to waiting on some kind of input. Usually you end up needing the result of this input for the next step in your code and thus the wait is necessary. However in some special cases (such as downloading a set of images), the inputs are not interconnected and therefore can be done concurrently. Normally this would mean learning and implementing some sort of multithreaded paradigm, which are usually tedious and fraught with risk.

However, as of .NET version 4.5, you can now use the simple and easy to use async keyword to modify your existing code to speed it up with the power of multithreading without making your code unreadable nor adding a lot of extra syntax.

For example consider this simple piece of code that fetches three images:

static void Main(string[] args)
{
    var microsoftLogo =
        GetImage(
            "http://c.s-microsoft.com/en-us/CMSImages/mslogo.png?version=856673f8-e6be-0476-6669-d5bf2300391d");
    var googleLogo =
        GetImage("https://www.google.com/images/srpr/logo11w.png");
    var yahooLogo =
        GetImage("https://s.yimg.com/nn/img/yahoo-logo-201402200629.png");

    Console.WriteLine(
        String.Format("Retrieved {0} total bytes.", microsoftLogo.Length + googleLogo.Length + yahooLogo.Length));
    Console.ReadKey();
}

static byte[] GetImage(string imageUrl)
{
    var client = new WebClient();
    byte[] data = client.DownloadData(imageUrl);
    Console.WriteLine(String.Format("Retrieved {0}.", imageUrl));
    return data;
}

 

This code will get the Microsoft logo first, and then the Google logo, and then finally the Yahoo logo. But with a few small tweaks we can make them retrieve at the same time:

static void Main(string[] args)
{
       var microsoftLogoTask =
               GetImage( 
                     "http://c.s-microsoft.com/en-us/CMSImages/mslogo.png?version=856673f8-e6be-0476-6669-d5bf2300391d");
        var googleLogoTask =
             GetImage("https://www.google.com/images/srpr/logo11w.png")
        var yahooLogoTask = 
             GetImage("https://s.yimg.com/nn/img/yahoo-logo-201402200629.png");

        var microsoftLogo = microsoftLogoTask.Result;
        var googleLogo = googleLogoTask.Result;
        var yahooLogo = yahooLogoTask.Result;

       Console.WriteLine(
              String.Format("Retrieved {0} total bytes.", microsoftLogo.Length + googleLogo.Length + yahooLogo.Length));
       Console.ReadKey();
}

static async Task<byte[]> GetImage(string imageUrl)
{
        var client = new WebClient();
        byte[] data = client.DownloadData(imageUrl);
        Console.WriteLine(String.Format("Retrieved {0}.", imageUrl));
        return data;
}

Now we’re retrieving these images asynchronously! They may come back in the same order, but they may not. The three calls to GetImage now kick off a download of each image without waiting for the image to actually finish, but the program will only finish when all 3 are retrieved. The key to this is using the Result property. This tells our program that the system now needs the value that the asynchronous function is already in the process of retrieving and waits until the function truly returns the value.

As in any multithreading model, we still have some basic considerations to keep in mind such as resource sharing and deadlocks, but with some forethought these problems are solvable, and with .NET 4.5, adding concurrency to your app is as simple as adding a few special keywords!

For more information on this topic, please see Microsoft’s documentation on async, and its partner keyword await.

When faced with a problem that needs to be solved with precision, no one would even consider not measuring carefully two, maybe even three times. Consider a carpenter building a bookcase the full length and height of a wall – he has absolutely no room to spare, he must be precise in the angles he cuts. Consider a seamstress tailoring a wedding dress. She cannot afford to miss-measure and risk ruining the bride’s big day. How about a doctor preparing to amputate an arm or leg? The amputee most certainly doesn’t want the doctor to give a haphazard measurement such as “Well I think right about there ought to do it!” This saying is true in many professions, not the least of which in the technology field. 

Technology fields are fields of science. No good scientist would ever consider flying by the seat of their pants, most especially not when proving a hypothesis. In the computing industry, when we sit down to our computers to solve wicked problems, we effectively take off our cap and jacket, put on a lab coat, and go about forming and proving hypotheses. Sometimes however, if not careful, people are tempted to start performing experiments before forming a hypothesis. This is quite nearly as dangerous as self-diagnosing a medical condition via Yahoo! Answers.

I.T. professionals must remember their job requires a scientific and analytical approach to solving problems. Companies that hire I.T. professionals must remember that getting to the bottom of a problem is a process and rushing that process or trying to self-diagnose the answer to the problem will most often result in an experiment blowing up in someone’s face.

So what does that process look like?

First, identify the problem. Ask some research questions:

  • How is the problem reproduced? 
  • Who reported the problem? 
  • Are there any errors being indicated by the system? If so, are any of those errors false negatives or alternatively is any one potentially the root problem?

Second, list some hypotheses (don’t do any experiments yet!) and gather data in preparation for experimentation. Data will be things like:

  • What is the average amount of time for X to run? 
  • What is the average number of times X occurs? 
  • What is the average size of X? 
  • How often or when does the problem occur? 
  • Is the problem/measurement the same regardless of where or who or when?

Third, based on the problem definition and the baseline metrics taken, begin proving or disproving your hypothesis with methodical changes (your experiments). After each change (sometimes after each step in making the change) re-measure and compare to the baseline metrics to verify if the change had an effect.

By following a methodical process to problem solving, the I.T. professional ensures that the correct problem and its root cause is found rather than taking a shoot first and ask questions later approach that may or may not actually solve the problem. At the end of the process, they are armed with information and real, honest proofs to show for their work and of course the solution to the problem.