This years code camp was over all pretty good. One of the highlights for me was Rocky Lhotka’s presentation on “Practical Parallelism”. He did a very good job presenting the ins and outs of parallel programming. Of the entire presentation the coolest part for me was a small little code sample he showed that changes the mouse cursor to an hour glass while a given operation was in progress.
Yes I know far from ground breaking! But the way it was implemented was pretty darn neat. Take a look at the class below.
public sealed class BusyCursor : IDisposable { #region Private Members private readonly FrameworkElement mFrameworkElement; #endregion #region Constructor /// /// Initializes a new instance of the class. /// /// The framework element. /// The busy cursor. public BusyCursor(FrameworkElement frameworkElement, Cursor busyCursor) { mFrameworkElement = frameworkElement; mFrameworkElement.Cursor = busyCursor; } #endregion #region IDisposable Members /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { mFrameworkElement.Cursor = Cursors.Arrow; } #endregion }
Ok great! Why is this so neat? I find the use of the IDisposable interface here pretty cool. This object will replace the cursor to a ‘Busy’ indicator when created, and switch it back for you when disposed! Lets look at how you could use this.
using (new BusyCursor(this as FrameworkElement, Cursors.Wait)) { Search(); }
Enjoy!,