Welcome to WindowsClient.net | Sign in | Join

I've just got my own hosting and decided to move my blog over there. All future posts can be found at blog.markrendle.net. Hope to see you there!

This entry presents an easy short-cut to implementing the Event-based Asynchronous Pattern in your classes, using the functional programming features of C# 3.0.

I'm currently building a framework to support a complex smart client application, and I'm trying to make good practices as easy to use as possible for the developers who use it. One facet of this is making multi-threading accessible, and the best way I've seen to do that is the Event-Based Asynchronous Pattern that was introduced in .NET 3.0.

In its simplest form, the pattern consists of a method and an event. The method executes on a background thread, and the event is raised when the method completes or throws an exception. The base class library provides an AsyncCompletedEventArgs type which includes an Error property to hold any exception thrown during the execution. For methods which return a value, that value is provided within the EventArgs; that's the pattern I'm going to cover here.

This simple form is another ideal candidate for a shortcut using delegates. A basic implementation might look like this:

public void GetUriAsync(Uri uri)
{
Action asyncAction = () =>
{
try
{
string text = GetUri(uri);
if (GetUriCompleted != null)
{
GetUriCompleted(this, new GetUriCompletedEventArgs(text));
}
}
catch (Exception ex)
{
if (GetUriCompleted != null)
{
GetUriCompleted(this, new GetUriCompletedEventArgs(ex));
}
}
};

AsyncCallback callback = (asyncResult) => asyncAction.EndInvoke(asyncResult);

asyncAction.BeginInvoke(callback, null);
}

public event GetUriCompletedEventHandler GetUriCompleted;

This code creates an Action delegate and uses its BeginInvoke method as a simple way to achieve the background threading. There is also a custom class, GetUriCompletedEventArgs, (not shown) which derives from AsyncCompletedEventArgs and provides a property for the text returned from the call, and a custom delegate for the event handler.

Casting a short-cutting eye over the code, it's fairly easy to see that the distinguishing parts are just the method being called, the type it returns, and the event handler; everything else is boilerplate. And methods, types and event handlers can all be passed as parameters.

Let's deal with the return type and the event handler first. Rather than declare a new EventArgs class and EventHandler delegate every time we implement this pattern, we can just create a single generic class and delegate which can be re-used every time:

public class AsyncCompletedEventArgs<T> : AsyncCompletedEventArgs
{
private readonly T returnValue;

public AsyncCompletedEventArgs(T returnValue, object userState)
: base(null, false, userState)
{
this.returnValue = returnValue;
}

public AsyncCompletedEventArgs(Exception error, object userState)
: base(error, false, userState) { }

public T ReturnValue
{
get
{
this.RaiseExceptionIfNecessary(); // Inherited from AsyncCompletedEventArgs
return returnValue;
}
}
}
public delegate void AsyncCompletedEventHandler<T>(object sender, AsyncCompletedEventArgs<T> e);

(Notice that the call to the base constructor passes false for the cancelled parameter. It's not really possible to implement a cancellable background process using this technique, as you would have to include cancellation awareness into the implementation code in the actual GetUri method.)

Now we can create a single helper method which will take any Func<T> delegate and execute it asynchronously:

public static void InvokeAsync<T>(Func<T> func, object sender, 
AsyncCompletedEventHandler<T> eventHandler, object userState)
{
// Wrap the passed Func in an Action delegate which raises the AsyncComplete event
Action asyncAction = () =>
{
try
{
// Call the supplied Func
T returnValue = func();

// Raise the AsyncCompleted event with the returned value
eventHandler(sender, new AsyncCompletedEventArgs<T>(returnValue, userState));
}
catch (Exception ex)
{
// Raise the AsyncCompleted event with the exception
eventHandler(sender, new AsyncCompletedEventArgs<T>(ex, userState));
}
};

// Use a callback to call EndInvoke on the Action delegate
AsyncCallback callback = (asyncResult) => asyncAction.EndInvoke(asyncResult);

// Start the Action delegate on another thread using BeginInvoke
asyncAction.BeginInvoke(callback, null);
}

This is the same technique as the custom-written GetUriAsync method above, but it takes a Func<T> delegate, which could do pretty much anything, and some extra parameters for the event handling process. The userState value is intended to identify which call to a method is raising the event in situations where multiple calls are made.

In this code, all the parameters are turned into closures within the asyncAction delegate, and the asyncAction delegate is itself passed as a closure into the callback delegate.

Consuming this helper looks like this:

public class Example
{
public string GetUri(Uri uri)
{
...

}

public void GetUriAsync(Uri uri)
{
AsyncHelper.InvokeAsync<string>(() => GetUri(uri), this, GetUriCompleted, uri);
}

public event AsyncCompletedEventHandler<string> GetUriCompleted;
}

A single line of code and an event are all that's now required to provide the pattern functionality. The call to GetUri(uri) is wrapped in a parameterless Func<T> delegate; this would work unmodified with methods with multiple parameters. We pass the Example object to be the sender of the event, the event handler, and use the uri object as the userState value so the caller can identify which method is "returning".

The other big advantage to using techniques like this is that in the future, you can change the implementation code in the helper method, maybe to use something in the .NET 4.0 Parallel Extensions, and all your consumer classes pick up the new implementation. And unlike C++ macros, you don't even need to rebuild anything except the Helper assembly.

I've included all this code, along with an alternative AsyncInvoke method which takes an Action delegate instead of a Func<T>, in the FunctionalHelpers solution on my MSDN Code Gallery page. From now on, I'll add any new code to this solution, rather than uploading lots of little projects.

We all have coding patterns that we like, and use a lot. Some of them can be quite laborious, though, so if you're anything like me you'll be looking for shortcuts. C++ programmers have a powerful macro language that can provide a shorthand for a complicated block of code, which is then expanded during the compile process. C# doesn't have a macro language, but using anonymous delegates and closures, it is possible to achieve something similar, maybe even better. I've come up with a few little tricks in this area, so I'm going to share them here over the next few updates.

This example looks at an optimisation technique wherein the return values from slow-running deterministic methods (i.e. methods which, given a certain parameter, will always return the same value) are cached locally, and returned from the cache on subsequent calls. The pattern looks like this:

public class Example
{
  private Dictionary<Uri, string> cache = new Dictionary<Uri, string>();
  private object lockObject = new object();

  public string GetFromWeb(Uri uri)
  {
    if (!cache.ContainsKey(uri)) // Check cache
    {
      lock (lockObject) // Thread safety!
      {
        // Check cache again in case key was added while waiting for lock
        if (!cache.ContainsKey(uri))
        {
          // Process request
          var webClient = new WebClient();
          var reader = new StreamReader(webClient.OpenRead(uri));

          // Add result to cache
          cache.Add(uri, reader.ReadToEnd());
        }
      }
    }

    // Return cached value
    return cache[uri];
  }
}

In this - admittedly simple - example, that's a poor ratio of boilerplate to actual code. On top of that, we've had to declare a couple of variables in the containing class which exist only to facilitate this optimisation pattern in this method. After typing out this pattern dozens of times (well, at least ten. OK, five. Fine, it was twice, I get bored quickly) I wondered if there was a better way. And there is.

First off, let's refactor the code above to extract the three lines that are actually doing the work and put them in their own method:

public class Example
{
  private Dictionary<Uri, string> cache = new Dictionary<Uri, string>();
  private object lockObject = new object();

  public string GetFromWeb(Uri uri)
  {
    if (!cache.ContainsKey(uri)) // Check cache
    {
      lock (lockObject) // Thread safety!
      {
        // Check cache again in case key was added while waiting for lock
        if (!cache.ContainsKey(uri))
        {
          // Add result to cache
          cache.Add(uri, ProcessRequest(uri));
        }
      }
    }

    // Return cached value
    return cache[uri];
  }

  private string ProcessRequest(Uri uri)
  {
    var webClient = new WebClient();
    var reader = new StreamReader(webClient.OpenRead(uri));

    // Add result to cache
    return reader.ReadToEnd();
  }
}

That's better code, for a start, but it also shows that all we're doing is storing the return value from a method in a dictionary, using the parameter as a key. If we represent the method as a Func delegate, it will have the exact same type parameters as the dictionary used for the cache. So we can create a generic wrapper around the method which handles all the boilerplate code, and just concentrate on our application logic.

We could create a class which exposed a Func<T, TReturn> and held the dictionary internally, but there's a feature in C# (since 2.0) which means we don't even need to do that: closures. Closures mean that when you create an anonymous method, any locally-scoped variables referenced by the method are bundled up with it automatically by the compiler in a generated class that you don't have to worry about.

This means we can just create a new delegate wrapped around the method we want to cache, and bundle its dictionary in with it as a closure. We can do this using a utility method which will work with any method matching the Func<T,TResult> delegate signature:

public static class MethodCacher
{
  public static Func<T, TResult> MakeCached<T, TResult>(Func<T, TResult> func)
  {
    // cache and lockObject will be bundled into the delegate as closures.
    var cache = new Dictionary<T, TResult>();
    var lockObject = new object();

    Func<T, TResult> cachedFunc = (arg) =>
        {
          if (!cache.ContainsKey(arg)) // check for cached value
          {
            lock (lockObject) // exclusive lock
            {
              if (!cache.ContainsKey(arg)) // re-check after obtaining lock
              {
                cache.Add(arg, func(arg)); // add return value from wrapped delegate to cache
              }
            }
          }

          return cache[arg];
        };

    return cachedFunc;
  }
}

You can use this utility method to create caching versions of methods you don't have the source for, or to switch caching on and off easily, maybe depending on the quality of network connection. A full example of this use is available from my MSDN Code Gallery page.

I hope this has been interesting and/or informative, and I'll post something in a similar vein soonish.

There's been a lot of discussion around type inference and the var keyword in C# 3.0 since it was released. Personally I'm a fan, as I never enjoyed having to type things like
 
using (SqlDataReader reader = cmd.ExecuteReader())
 
(Intellisense is great, but there are so many types that start with "SqlData" that it was still annoying.)
 
Anyway, I'm working on a business object framework at the moment, have been for a long time, and as I go along I refactor a lot for best practices. I've just been through and looked at all the methods that return arrays to check whether that's actually the most appropriate type; usually, it isn't. I found a lot of places where the only functionality possibly required by the consumers was iteration or LINQ-related, meaning the correct return type was IEnumerable<T>. Since, for example, "string[]" implements IEnumerable<string>, I could very quickly change the return type of the method and worry about the implementation, if necessary, later.
 
In about half the instances where I made this change, the references to the method elsewhere in the code were pre-C# 3.0, so they explicitly used the array type as the variable declaration and I had to go through and change them. Naturally, I changed them to var, which was a hell of a lot easier than specifying the actual IEnumerable<T> in every case.
 
In the other half of the references to these methods, the ones written post-C# 3.0, I'd used var already. And in all except one case, after I'd changed the return type of the method, the calling code required no modification at all. That one case involved a "x.Length > 0" check and an "IndexOf(x,y) > -1", which were pointed up via compilation errors and swiftly changed to "x.Any()" and "x.Contains(y)".
 
So, to the extent that consumers are less interested in the specific type being returned than in the properties and methods it exposes, using type inference can make refactoring considerably easier. That's a good argument for me.

So I was reading Angry Hacker's 10 reasons why SQL 2008 is going to rock and one of the reasons was compression, and one of the things about compression was that:

For instance, length of the varchar will be stored in 3 bits.

That bothered me. 3 bits represent a value from 0 to 7. How can that store all the possible lengths of a varchar, up to 8192? I kind of had the problem running in background all last night (at the same time as learning Ruby and watching the BBC's Bill Gates documentary; who says men can't multitask?). Finally, this morning, I came up with a theory: it's a lookup table.

Looking at the database I primarily work with, we only actually use a handful of different varchar sizes. 80% of the text columns are varchar(254), due to a limitation of a legacy development system's ODBC implementation. There are a few 50s where people probably just forgot to change the default in the table designer. For some reason there are a lot of 40s. And there are a bunch of isolated odd lengths, which I'll probably do something about if purchasing approve my request for VSTS Database Edition. Point is, >95% of the columns are covered by the top 7 values. So those 7 values could be held in an int[7] somewhere, and the actual metadata stored as the index to that table. The few extraordinary values are stored as regular ints (you use the remaining 3-bit value to indicate that happening).

Of course, that's just my database. I don't know what other people's databases are like. But Microsoft do. Microsoft have got the Customer Experience Improvement Program. I'd always wondered about the kind of information that they collected; I'm guessing that distributions of data types and lengths is in there somewhere. They've got enough information about enough usage patterns to be able to do stuff like this. So now I'm thinking about adding my own CEIP to my company's applications, just to see if there are any obvious optimisations waiting to stare me in the face.

Disclaimer: as I say, this is purely guesswork. But if anyone from the SQL team reads this, maybe you could let me know whether I'm warm or freezing?

Jared Parsons posted about using enums as a simplified version of the adapter pattern. In his code, each time the SomeAction method is called, it runs a switch against the private enum value and calls on to the relevant method.

Since C# 3.0 came along, with its lambdas and functional-style abilities, I've gotten used to thinking of methods as variables, which presents another way to do this sort of thing. Rather than having a public method, you can expose a delegate through a public property, and set the delegate according to the required behaviour. Using this method, Jared's example could look like this:

class Example
{
    public Example(Kind kind)
    {
        switch (kind)
        {
            case Kind.Kind1:
                this.SomeAction = ActionForKind1;
                break;
            case Kind.Kind2:
                this.SomeAction = ActionForKind2;
                break;
            case Kind.Kind3:
                this.SomeAction = ActionForKind3;
                break;
            default:
                throw new InvalidOperationException("Invalid Kind");
        }
    }

    public Func<int> SomeAction { get; private set; }

    private int ActionForKind1() { return -1; }

    private int ActionForKind2() { return 0; }

    private int ActionForKind3() { return 1; }
}

I think that's just as readable. This technique can be used for more dynamic behaviour switching. Next time, I'll post a very simple technique for creating a delegate wrapper for long-running deterministic functions which caches in memory. Once you've got that, you can use this delegate property technique for switching between the cached and non-cached versions.

Enough is enough. I got 40-odd spam comments over the weekend, and another 8 moderation emails just landed in my inbox. Goodness only knows what it would be like if I was actually updating as often as I'd like.

Hopefully some new posts about functional programming soon...

I'm fortunate enough to be attending DevWeek 2008 this week. Today I took in 3 WPF-related sessions by Dave Wheeler (who also presented yesterday's excellent keynote on Silverlight). The talk that was of the most interest to me, and probably to many other people, covered using WPF and Windows Forms together. My current project has got far too much Windows Forms code to consider a re-write at this stage, but there are a couple of interface features I'd like to implement which will be much easier in WPF. It seems this is fairly straightforward (unlike interop between WPF and Win32/MFC, which made me glad I left all that behind).

One of the caveats Dave mentioned was that the ShowDialog method in the Windows Forms Form class requires an IWin32Window type as its argument, and WPF controls don't implement that type or, indeed, have an IntPtr handle. He presented a shim solution which uses the WindowInteropHelper to provide a surrogate handle for WPF Window types:

class Shim : IWin32Window
{
  public Shim(System.Windows.Window owner)
  {
    // Create a WindowInteropHelper for the WPF Window
    interopHelper = new WindowInteropHelper(owner);
  }

  private WindowInteropHelper interopHelper;

  #region IWin32Window Members

  public IntPtr Handle
  {
    get
    {
      // Return the surrogate handle
      return interopHelper.Handle;
    }
  }

  #endregion
}

This set off my current obsession with extension methods, so I scribbled one to wrap this technique, tested it when I got home and it worked fine. Here it is:

namespace System.Windows.Forms
{
  public static class WPFInteropExtensions
  {
    public static DialogResult ShowDialog(
        this System.Windows.Forms.Form form,
        System.Windows.Window owner)
    {
      Shim shim = new Shim(owner);
      return form.ShowDialog(shim);
    }

    class Shim : IWin32Window
    {
      // as above
    }
  }
}

I've lost count of the number of times I've written recursive functions to iterate through all the controls on a form, including those contained in panels or group boxes or group boxes in panels. But hopefully, thanks to C# 3.0 and extension methods, I've just written the last one.

/// <summary>
/// Provides extension methods to the 
///
<see cref="System.Windows.Forms.Control.ControlCollection"/> type. /// </summary> public static class ControlCollectionExtensions { /// <summary> /// Adds a method to recurses through the specified control collection,
/// including the controls within controls etc.
/// </summary> /// <param name="controlCollection">The control collection.</param> /// <returns>An <see cref="IEnumerable&lt;Control&gt;"/>.</returns> public static IEnumerable&lt;Control&gt;
Recurse(this Control.ControlCollection controlCollection) { foreach (Control item in controlCollection) { yield return item; if (item.HasChildren) {
// The recursion point... foreach (var subItem in item.Controls.Recurse()) { yield return subItem; } } } } }

And because this returns an IEnumerable<T>, it's fully Linq-able, which means I can formulate queries such as:

var q = from control in Controls.Recurse()
        where control.DataBindings.Count > 0
        select control;

(which will give me every control with any data-binding happening to it).

I am deeply, deeply happy with C# 3.0.

I haven't written many web services up to this point in my life, although I'm about to lead the development of a suite of services intended as an API for our solution. Because of this, the experience I've just had bothers me.

I knocked together a very simple web service for a client, with two methods, one to get some data and one to perform an update. They appeared to work fine, until they got down to some integration testing, at which point they started to throw intermittent exceptions.

A little bit of playing about from a Console app revealed that you could call the querying service as much as you liked, but as soon as you called the update service, the querying service stopped finding rows in the table. Obviously this is all remote and not on our servers, so debugging was basically impossible. I've spent the afternoon adding explicit transactions to the code, changing static declarations to instance, setting the TransactionOption property in the WebMethod attribute, all sorts. Finally my increasingly random searching threw up a 15 Seconds post about connection pooling, so I added "Pooling=False" to the connection string in the config file and that, annoyingly, fixed it.

I'm doubly annoyed now because I've made the problem go away without actually solving it: I don't know why or how pooling was causing the problem. There was no mention of transactions in the code to start with (it's a single update statement in a stored procedure), and when I added a transaction it was committed or rolled back immediately. All the SqlConnection, SqlCommand and SqlDataReader variables were neatly wrapped in using statements, so they should have been dead and gone by the time the service was recalled.

If anybody understands this better than me, please send your explanation on a postcard to "Confused, Tunbridge Wells".

JetBrains have started the Early Access Program for the new version of their ReSharper tool. 4.0 adds proper support for C# 3.0 features, stops highlighting the new syntactic sugar statements in red, and improves on a bunch of other features too. All the usual caveats about this being unstable software apply, but the best way to get it stable fast is to get using it and submitting issues through their JIRA database,

I only really discovered ReSharper recently while looking for a tool to automatically extract strings from code to a resource file, which the RGreatEx add-in does very nicely along with a bunch of other stuff. Don't know when they'll have a 4.0 compatible version out.

I was watching the Alex Heinrichs: Shipping Windows Server 2008 Channel 9 video on the train this morning, and he was talking about the most exciting feature being Server Core, which is probably true. And I got to thinking about WS2008 being Windows 6.1, and Vista SP1 being essentially the same kernel, and I went off on a mental tangent about Client Core, and whether that was an actual thing.

I came to the conclusion that it probably is, and I think that with Windows 7 we might see Client Core installs as an option. I can think of two applications off the top of my head: thin clients and web browsers. A desktop PC with only Remote Desktop installed could be low-spec and would be low-maintenance in terms of installation and patching, and there'd be the same "reduced attack surface" advantage that Server Core has.

Similarly for environments where a web browser is the only application - public internet points, those online-banking PCs in bank branches, interactive systems in shops - not having the rest of Windows installed would be cool. Although it would probably reduce the number of publicly visible blue screens of death in the world, which would be a bit of a shame...

Hi, I'm Mark. I'm a software architect/lead developer/product manager at a large software company in the UK. I'm currently leading the redevelopment of one of our major packages in .NET, and we've just switched over to VS2008 and Framework 3.5. It's a pretty good job, really, and I'm getting to do lots of problem-solving and framework engineering sorts of things along the way, so I thought I'd share some of the stuff I'm coming up with. Maybe some of it will be helpful. Maybe some of it will be wrong, and if it is, maybe you good people can let me know in the comments?

Also, random thoughts and links along the way. One coming up next...

 
Page view counter