Using Async and Await to update the UI Thread Part 2

In a previous article on async and await, I showed a very simple example of how to run some code asynchronously. Then in the 2nd article I showed an example of updating the user interface in the main thread from an async method.

The code below (from the previous article) would execute a long running task which in this case counts to 5,000,000. As the task was running, on a set interval, the UI would be updated. This effectively decoupled the running of the task from the updating of the UI. You could have the task update the UI every 10ms, or you could update every 5 seconds. It really depends on what you are trying to do.

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsAsync
{
    public partial class Form1 : Form
    {
        private readonly SynchronizationContext synchronizationContext;
        private DateTime previousTime = DateTime.Now;

        public Form1()
        {
            InitializeComponent();
            synchronizationContext = SynchronizationContext.Current;
        }

        private async void ButtonClickHandlerAsync(object sender, EventArgs e)
        {
            button1.Enabled = false;
            var count = 0;

            await Task.Run(() =>
            {
                for (var i = 0; i <= 5000000; i++)
                {
                    UpdateUI(i);
                    count = i;
                }
            });

            label1.Text = @"Counter " + count;
            button1.Enabled = true;
        }

        public void UpdateUI(int value)
        {
            var timeNow = DateTime.Now;

            if ((DateTime.Now - previousTime).Milliseconds <= 50) return;

            synchronizationContext.Post(new SendOrPostCallback(o =>
            {
                label1.Text = @"Counter " + (int)o;
            }), value);             

            previousTime = timeNow;
        }
    }
}

A reader on Reddit suggested that you can write this code in a much more succinct way as shown below. The code is much simpler to see what’s going on, but the behaviour is slightly different. In this example, the code similarly counts upto 5,000,000 and updates the UI label. The await statement here will correctly restore the synchronisation context to update the UI thread which means you don’t have to deal with it manually (I didn’t realise that at the time). To enable the asynchrony of the task in this example we need to have a Task.Delay(1) as we are not using any other asynchronous objects in .NET for file or DB access for example.

Using Async and Await to update the UI Thread

In a previous article on async and await, I showed a very simple example of how to run some code asynchronously. In this article, I want to take this a little further with another example.

I have recently had to go and maintain an old c# Winforms application (yes I know winforms is a little old and considered the new vb6 but the application works and still provides value) that needed some new features adding. The problem with this application is that is feels a little sluggish for the user and constantly locks up, or gives the impression that it has crashed which means users have been trying to kill the application when really, it is processing a lot of work and the UI has become unresponsive.

Whilst I was in the code base, I wanted to try and improve some of this unresponsiveness by moving code out into tasks and asynchronously updating the UI. In this article I will cover how to do this.

Example of Unresponsive Application

First lets start with a small example. Below is a screen shot of a very simple winforms app. It doesn’t really do much. When you load it up, you press the button and the application counts to 5,000,000. I want this count to be displayed on the screen.

Simple Application for an Async and Await Example
Simple Application for an Async and Await Example
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void ButtonClickHandler(object sender, EventArgs e)
    {
        for (var i = 0; i <= 5000000; i++)
        {
            label1.Text = @"Counter = " + i;
        }
    }
}

In the code above, when you click the button in the application it calls the ButtonClickHandler which starts a count to 5,000,000. Inside the for loop we also try to update the text on the label to tell the user how far the count has got.

Simple Async Await Example for Asynchronous Programming

In this article I want to give a simple example for how to use the async await keywords in C# to create asynchronous background tasks.

Writing multi-threaded or asynchronous code has traditionally always been very hard to get right but is something that is needed to help keep our applications responsive and to avoid performance bottlenecks. C# 5 introduced a simplified model for doing asynchronous programming with the introduction of 2 new keywords, async and await.

If you specify that a method is an asynchronous method by using an async modifier, you enable the following two capabilities.

  • The marked async method can use the await keyword to designate suspension points. The await operator tells the compiler that the async method can’t continue past that point until the awaited asynchronous process is complete. In the meantime, control returns to the caller of the async method. The suspension of an async method at an await expression doesn’t constitute an exit from the method, and finally blocks don’t run.

  • The marked async method can itself be awaited by methods that call it.

An async method typically contains one or more occurrences of an await operator, but the absence of await expressions doesn’t cause a compiler error. If an async method doesn’t use an await operator to mark a suspension point, the method executes as a synchronous method does, despite the async modifier. The compiler issues a warning for such methods.

Asynchrony is essential for activities that are potentially blocking, such as when your application accesses the web or a file system. Access to a web resource, for example, is sometimes slow or delayed. If such an activity is blocked within a synchronous process, the entire application must wait. In an asynchronous process, the application can continue with other work that doesn’t depend on the web resource until the potentially blocking task finishes.

Password Based Key Derivation Functions in .NET

In this article I want to talk a little about Password Based Key Derivation Functions and their use in .NET. A Password Based Key Derivation Function or PBKDF2 as it is known, is a way to encode passwords as an alternative to hashing functions which are susceptible to rainbow table attacks.

Password Based Key Derivation Functions in .NET : PBKDF2
Password Based Key Derivation Functions in .NET : PBKDF2

For this article though I am going to try a different method of explaining it. I am going to talk about this subject and do a little code demo by video. Yes, you have to suffer my voice and video editing. This video was actually recoded back in June, and the astute amongst you will notice that this looks very much like a Pluralsight video. Well, it is, kind of. This is one of my audition videos that I had to produce to  become a Pluralsight author.

I was very pleased with the result seeing as it was my first time recording and editing a video / code demo, and Pluralsight were gracious enough to give me permission to post the video on my blog, but minus the Pluralsight branding, as it is not an official video of theirs.

Now that I have the video recording bug, plus I have paid for all the software and hardware etc, I may do more of these along side my Pluralsight courses.

Remove the Close Button from a WPF Window.

Recently for a bit of fun I thought I would play around with Windows Presentation Foundation. I come from a Winforms background, but I never really got into WPF development as my programming career steered more towards WCF Services development.

For my simple project I thought I would write a simple little game. The game is a board game where the game resides in the main application window. What I wanted to have was another window that pops up and contains the games main menu, with options like ‘New Game’, ‘Exit’ etc.

WPF Window With Close Button
WPF Window With Close Button

When I added the code and Xaml to define the window, I noticed that the window still had a close icon on it. I wanted to get rid of this because I wanted access out of the menu to be controlled by the menu items in the centre of the screen. This was easy to do in Winforms as there was an option for it in the Visual Studio designer. Unfortunately this is not easily supported in WPF, but after a little digging I found the solution which required a little interop into user32.dll.

Machine Learning with Numl

I came across a really good library recently called Numl. Numl is a .NET library that aims to make Machine Learning much more accessible to developers by abstracting away all the complex parts. What is Machine Learning I hear you say. Machine Learning can be described as (and this is a quote from the Numl site).

The purpose of machine learning is to find (and exploit) patterns in data. Traditionally developers, when faced with a problem, develop and algorithm and write code. Certain classes of problems, however, do not lend themselves to this approach. With machine learning, the developer instead supplies relevant data to the machine and allows the computer to create the appropriate algorithm.

Numl does this via supervised and unsupervised learning. These are :

Machine Learning with Numl
Machine Learning with Numl
  • Supervised Learning : Supervised learning is the branch of machine learning that deals primarily with prediction. Given examples, supervised learning algorithms create models that generalize the decision making process. In essence, the machine learns from the past in order to accurately predict the future.
  • UnSupervised Learning : Unsupervised learning is the branch of machine learning that strives to understand the structure of data. This data, unlike supervised learning, does not have a predefined outcome that requires prediction but is vast enough to require a principled approach to either visual or physical compression.

Supervised learning is that part that I find most interesting and useful for my purposes. When using supervised learning you provide a labelled set of examples, and this can be provided as lists of objects, data tables etc. These data sets contain examples of how decisions were made previously.

A Software Developer’s Guide to HTTP

I came across an excellent series of articles on a Software Developers Guide to HTTP over on Scott Allens blog, Ode To Code.

Http Programming

The series covers subjects such as Resources, Messages, Connections, Architecture and Security. It’s a good worthwhile course. The links to each part are here :

Part I: Resources
Part II: Messages
Part III: Connections
Part IV: Architecture
Part V: Security

Scott us also a Pluralsight author who has developed some very good courses on web development and general development.

Code Example : Quick and Easy Password Generator

As part of my new SafePad 1.3 release, I added a simple tool to help users generate passwords. I looked into many ways of generating secure passwords, but in the end I settled on a technique that was very simple, quick to implement, and produces complex to crack random passwords.

Safepad is commonly used as a password vault for storing passwords for multiple sites in one document. The document is then protected with up to 2 passwords. One feature that was requested was that of generating complex passwords straight into a SafePad document.

The solution I picked is demonstrated below. In SafePad, the maximum password length is restricted to 64 characters.

using System;
using System.Security.Cryptography;

namespace HauntedHouseSoftware.SecureNotePad.Tools
{
    public static class PasswordGenerator
    {
        public static string Generate(int passwordLength, bool singleCase)
        {
            if (passwordLength == 0)
            {
                throw new InvalidOperationException("passwordLength");
            }

            string password;

            using (var randomNumberGenerator = new RNGCryptoServiceProvider())
            {
                var randomNumber = new byte[64];
                randomNumberGenerator.GetBytes(randomNumber);

                password = Convert.ToBase64String(randomNumber);
            }

            password = password.Substring(0, passwordLength);

            if (singleCase)
            {
                password = password.ToLower();
            }

            return password;
        }
    }
}

A coding requirement was to generate passwords that where not repeatable, and to do this I used the RNGCryptoServiceProvider class in .NET. I have already discussed using RNGCryptoServiceProvider in a previous article.

Code Example : Checking Windows Service Status in C#

Sometimes it is useful to be able to check for the existence of a windows service on a local or remote machine and also check the running status of that service. Doing this is very easy in .NET. The following snippets are taken from the windows services tests in the Post Deployment Smoke Tester.

var ctl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "MyService");
if (ctl == null) throw new AssertionException(string.Format("Service with name [{0}] was not found", "MyService"));

The snippet above will check for a named service on the local machine. You can also look for the existence of a service on a remote machine, but supplying the machine name in the call to GetServices, as shown below.

var ctl = ServiceController.GetServices("CENTLPBZBHV1").FirstOrDefault(s => s.ServiceName == "MyService");
if (ctl == null) throw new AssertionException(string.Format("Service with name [{0}] was not found on {1}", "MyService", "CENTLPBZBHV1"));

Code Example : Pinging a Remote Computer

In this short article I want to show a useful code snippet that allows you to ping a remote computer, much like the command line tool ping.exe. This particular code snippet was taken from a Smoke Test from the Post Deployment Smoke Testing tool that I am the open source moderator for.

Usage of the PingHost method is very straight forward. You just call it and pass in a host name. In the example below that is “www.google.co.uk”, but it could be a server name or ip address.

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace PingTester
{
    class Program
    {
        static void Main(string[] args)
        {
            string reply = PingHost("www.google.co.uk");
            Console.WriteLine(reply);
        }

        private static string PingHost(string host)
        {
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException("host");
            }

            var returnMessage = string.Empty;

            try
            {
                var address = GetIpFromHost(host);
                var pingOptions = new PingOptions(128, true);
                var ping = new Ping();
                var buffer = new byte[32];

                for (var i = 0; i < 4; i++)
                {
                    try
                    {
                        var pingReply = ping.Send(address, 5000, buffer, pingOptions);

                        if (pingReply != null)
                        {
                            switch (pingReply.Status)
                            {
                                case IPStatus.Success:
                                    returnMessage = string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}",
                                        pingReply.Address, pingReply.Buffer.Length, pingReply.RoundtripTime,
                                        pingReply.Options.Ttl);
                                    break;
                                case IPStatus.TimedOut:
                                    returnMessage = "Connection has timed out...";
                                    break;
                                default:
                                    returnMessage = string.Format("Ping failed: {0}", pingReply.Status);
                                    break;
                            }
                        }
                        else
                            returnMessage = "Connection failed for an unknown reason...";
                    }
                    catch (PingException ex)
                    {
                        returnMessage = string.Format("Connection Error: {0}", ex.Message);
                    }
                    catch (SocketException ex)
                    {
                        returnMessage = string.Format("Connection Error: {0}", ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Connection failed for an unknown reason...");
            }

            return returnMessage;
        }

        private static IPAddress GetIpFromHost(string host)
        {
            var address = Dns.GetHostEntry(host).AddressList[0];
            return address;
        }
    }
}

First of all a call is made to GetIpFromHost, which in turn call the .NET Dns.GetHostEntry method to resolve to an IP address. Then a call is made to Ping.Send(). At this point you will either get a success status or a timeout.  One thing to note though is that some enterprises have Ping disabled using group policy. If this is the case then you will get a “Connection has timed out error.”

%d bloggers like this: