Carlos Blé & Associates

Carlos Ble

We solve problems

  • Count on us to deliver high quality tailor-made software
  • We provide training on TDD, clean code, refactoring and JavaScript
  • We are committed consultants
  • We help teams deliver value

  Developing software since 2001

Archive for October, 2007

Nice blocking UI

Boxerp has a responsiveness engine that lets you launch operations in background easily as displaying some kind of message to the user along with an option to cancel the call. However, there are times in which the operation is supposed to be fast so you don't launch a different thread but use the current one.
What happen then if the operation is delayed for any reason?. I think is nicer to show a message in the meanwhile, so I came up with this idea:

   1:  using (new WaitMessage())
   2:  {
   3:    // my potentially blocking code here
   4:  }
And the class for WPF:
   1:      public partial class WaitMessage : System.Windows.Window, IDisposable
   2:      {
   3:          public WaitMessage()
   4:          {
   5:              InitializeComponent();
   6:              Show();
   7:          }
   9:          #region IDisposable Members
  11:          public void Dispose()
  12:          {
  13:              Close();
  14:              GC.SuppressFinalize(this);
  15:          }
  17:          // Deconstructor: make sure the window is disposed
  18:          ~WaitMessage()
  19:          {
  20:              Dispose();
  21:          }
  22:          #endregion
  23:      }

I haven't stress the window and I need to make sure the garbage collection is going to be ok but it is working so far. The window opens at the begining of the block displaying a message so that the user knows the application is supposed to be working ("Please wait..."), and it is closed at the end of the block. I'm going to add this feature to Boxerp making it extensible for Winfows Forms and Gtk#

When you're learning something new and eventually you figure it out, you got a fresh algorithm or rules in your mind, to understand how to use that. At that point you can explain it to somebody else and he/she will most likely understand the thing faster. However, when you become an expert your brain works automaticly and it is more difficult for you to explain it to somebody else. You forget that it is not straigforward to figure that out when you don't know it yet. Somebody else might be yourself within months, after a while without using that thing. This is a another good reason to write documentation and unit tests as the functionality is being implemented, that is, as the code is being written.

I had a bunch of ideas on how to explain the rationale behind applying generics when I was learning them. Now I just use strongly type structures and generic collections as much as I can but I would find it difficult to explain.

I'm know trying to leverage "new" features of C# and this time I'll post my thoughts about them. One useful manner of taking advantage of Anonymous Delegates is to use them as event handlers.

   1:          private void createDynamicPlaceholders()
   2:          {
   3:              StackPanel panel = new StackPanel();
   4:              panel.HorizontalAlignment = HorizontalAlignment.Left;
   5:              panel.Orientation = Orientation.Horizontal;
   7:              Label pickLabel = new Label();
   8:              pickLabel.Content = "Pick ";
   9:              Label ofLabel = new Label();
  10:              ofLabel.Content = " of ";
  12:                  // LOOK THIS:
  13:              IntegerTextBox cumulativeBox = new IntegerTextBox();
  14:              IntegerTextBox maxPicksBox = new IntegerTextBox();
  15:              maxPicksBox.KeyUp +=
  16:                  delegate(Object sender, KeyEventArgs args)
  17:                  {
  18:                      cumulativeBox.MaxValue = maxPicksBox.Integer;
  19:                      if (cumulativeBox.Integer > maxPicksBox.Integer)
  20:                      {
  21:                          cumulativeBox.Integer = maxPicksBox.Integer;
  22:                      }
  23:                  };
  25:              panel.Children.Add(pickLabel);
  26:              panel.Children.Add(cumulativeBox);
  27:              panel.Children.Add(ofLabel);
  28:              panel.Children.Add(maxPicksBox);
  30:              _dynamicPanel.Children.Add(panel);
  31:          }

If there wasn't an anonymous delegate I'd need a subscriber method but more importantly, I'd need to save the reference to the two textboxes, as the code in the subscriber needs them. It would imply having some kind of structure in memory to reference them, it means more lines of ugly code.