Software dev, tech, mind hacks and the occasional personal bit

Bounded Actions Using Lambda – IDisposable is old and ugly!

In .NET 2, it was all the rage to make hand-crafted, clever IDisposables that let you do a bounded action with clean up. Eg,

void SomeMethod()
{
        using (new SetCursorToWaitEggTimer())
        {
            VerySlowOperation();
        }
}

void VerySlowOperation()
{
    ... etc ...
}

This was kind of cute – you could make sure that, even if an exception was thrown, your clean up (eg, changing cursor back to normal) would occur. Implementing the IDisposable was a bit ugly but consuming it wasn’t bad.

Now, with the sexy C# 3 syntax, you can do something similar much more elegantly. Eg,

void SomeMethod()
{
       DoWithWaitEggTimer(VerySlowOperation);
}

void DoWithWaitEggTimer(Action action)
{
    try
    {
        Mouse.OverrideCursor = Cursors.Wait;
        action();
    }
    finally
    {
        Mouse.OverrideCursor = null;
    }
}

If you’re feeling like more adventures, you can also start passing these delegates around and injecting them. For example:

class SomeClass
{
    public Action RunSlowCode 
    {
    	get { return runSlowCode ?? new Action(a => a.Invoke()); }
    	set { runSlowCode = value; }
    }
    Action runSlowCode;

    void DoSomethingSlow()
    {
         RunSlowCode(PullDataFromExternalSystem);
    }
}

This approach allows you to inject the delegate for what happens when slow code is run. So you could inject DoWithWaitEggTimer() or something new like DoWithWaitMessageDisplayedToUser(). Similarly, it could be used for unit testing or injecting between layers in your application.

Previous

C# Default Access Modifier for Class Members – and drop that private habit!

Next

Photos from Sapa, Vietnam

2 Comments

  1. I think you are missing the invocation of the action in DoWithWaitEggTimer

  2. You’re right, thanks! Fixed now 🙂
    James

Powered by WordPress & Theme by Anders Norén