Thursday, January 12, 2006

Many applications have scaling, or zoom-in, zoom-out features of some kind.  Acrobat reader, Word, Graphics programs, etc.  We have these great Skin and Theme features in ASP.NET 2.0 but not so in Windows forms.  You can enable XP visual styles though, check out this Help article:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VSADD.v10.en/dv_vstechart/html/vbtchUsingWindowsXPVisualStylesWithControlsOnWindowsForms.htm

While trying to determine a good design approach to creating a skin-able windows app I stumbled across some odd behavior which I assumed must be a bug:

  1. Create a windows forms app and throw some controls on it.  Leave everything the default size/font/color since I am going to apply a skin to it anyway.
  2. At runtime, change the font to the "skin" font.
  3. If the design-time font was good ole MS sans-serif 8.25pt and I assigned a font with a different size (Verdana, 12pt) the form would suddenly get tremendously larger.

My forms extend from a BaseForm, so this must be a Visual Ineritence bug, I thought, and the client decided not to spend $$ on the skinable feature just yet anyway.  When a request came in to allow parts of the application to Scale via zoom-in, zoom-out, I came back to this.  My odd resizing forms are exhibiting behavior as designed.

ScaleMode

Windows Forms in .Net 2.0 are designed to automatically scale under one of two events: the Font assigned to the form changes, or the screen resolution changes.  There is an AutoScaleMode property on a Form, with the enumerated values being

  • None
  • Inherit
  • Font
  • DPI

It seems I could find my way through this in order to meet my scaling requirement and also revisit the Skin feature in future.  A Form keeps track of what dimensions it was designed to regardless of other resizing and such.  This is stored in the AutoScaleDimensions property.  From the set_Font method of a Form, the Scale and ultimately ScaleControl methods will be called.  These can be overridden to alter scaling behavior, or just observed to determine how to manipulate scaling.  So, I have an MDI application and the area I want to scale will be contained in a "ScalableForm" like so:

Now, a simple way to give the user the ability to zoom out, I create a simple ScaleData class.

public class ScaleData
{
public ScaleData(float size, string desc)
{
_fontSize = size;
_display = desc;
}
private float _fontSize;

public float FontSize
{
get { return _fontSize; }
set { _fontSize = value; }
}
private string _display;

public string Display
{
get { return _display; }
set { _display = value; }
}
}

... and populate some scale values in the ScalableForm constructor:

public ScalableForm()
{
InitializeComponent();
ScaleSizes = new List<ScaleData>();
ScaleSizes.Add(new ScaleData(this.Font.Size / 2f, "50%"));
ScaleSizes.Add(new ScaleData(this.Font.Size, "100%"));//This will be the design-time size
ScaleSizes.Add(new ScaleData(this.Font.Size * 2f, "200%"));

...

There is certainly no need to pre-calculate a limited set of zoom values but it removes the need for calculations later to get back to 100% .  Now I provide a menu item that will scale the currently active child Form and the code is very simple:

private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
  _scale.Font = new Font(_scale.Font.FontFamily,_scale.ScaleSizes[0].FontSize);
}

So telling it to zoom to 50% looks like:

A couple of other things bear mentioning.  Note that "a bunch of text in a label" appears the same size in both cases , although the Location of the Label was scaled down.  By default the ScaleChildren called from ContainerControl will not scale controls set to be automatically sized, so in this case I would need to tell my label not to auto size itself.  Also, the documentation for scaling claims you can have a control on this form not ever scale itself by extending a control class and providing the following:

public class UnscalableComboBox : ComboBox
{
   protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
   {
      base.SetBoundsCore(x, y, 121, 21, BoundsSpecified.None);
   }
}

However, neither this, not setting Locked=true, not setting MaximumSize and MinimumSize would stop a control from being scaled when I changed the font.  For now I put the scaling function in the menu (rather than a combo box on this form somewhere) but will continue to investigate this feature as I work on this project.

 

Thursday, January 12, 2006 10:51:44 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Wednesday, January 11, 2006

My presentation at the user group last night went ok.  A big part of my example was to show both multi-threading and mobile data synchronization with a mobile device talking wirelessly to my laptop.  The Wifi connection in the room was weak sauce though and I didn't have the foresight to bring an access point as a backup plan despite owning 2 or 3.  Seeing a progress bar tick off across the screen often makes things click for people, so that's too bad.  I have not presented in this building before either and wasn't quite prepared for how difficult it was to see what was on the screens.  Oh well, live and learn, I will definately scope out the Venue more before I do this again.

As soon as I get a moment I'll make the code and slides available here as well as from the Wineta site.

Wednesday, January 11, 2006 10:05:04 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, January 03, 2006

I posted some 2006 goals/resolutions on my personal site, but as I sit here on my 3rd cup of coffee I realize I forgot the most important one for 2006: get into work earlier.  Get up when the alarm goes off.  Stephen Covey calls this "Mind over Mattress" and quite rightly names it one of the most fundamental things you can to do Get Stuff Done.

I have never been a morning person.  This goal has been a failed resolution in years past.  Usually having kids forces people to shift to an earlier schedule but my almost 3 year old seems to have inherited mommy & daddy's desire to sleep in a little bit.  She did wake me up at 7:20 today to let her watch Spongebob, though.  I thought moving further out of the city would help too, but I still get up just early enough to get into work at a semi-reasonable hour.

This year will be different because I really have no choice.  I have enough to do in the month of January alone that it will not happen without some productivity changes on my part.  I'm such a slacker right now ;)  As soon as the Mrs. goes back to nursing clinicals later this month I have no choice but to get up stupid early at least three days a week to help with the daycare shuffle.  Let's hear it for Mind over Matress in 2006.

Tuesday, January 03, 2006 9:26:49 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [3]  |  Trackback
 Sunday, January 01, 2006

Despite various studies indicating the negative effects on employee productivity and especially morale my current client has purchased "Websense" to do detailed internet site blocking and surf-volume reporting.  I wonder how much money it cost them to reduce their employee productivity and job satisfaction in this manner?  Why does this bother people?  I suspect a top reason might be that most websites are not quite as ubiquitous as a phonecall.  Many firms have reviewed phone records for years and too much tele time might get you a stern talking to.  Your manager does not necessarily know what you called your wife 17 times a day about, just that you did.  Visiting websites, though, might give various keys as to your political beliefs, religious affiliations, and various other things someone could hold against you.  One would think that in the Litigious States of America this is knowledge employers would not want.  Now someone can claim they were let go because their manager snooped their internet habits and found out that they were gay, had HIV, was an atheist, voted Democrat, etc.

At any rate I've had to cut everything but MSDN and Fox News out of my daily diet in an effort to stay off of the top daily surfers list.  I typically have other work to do at home and the holidays have not given me more free time.  I finally set down and read/skimmed several hundred articles worth of waiting content.  To my fellow Wisconsin bloggers: I am caught up now.

I will be having a Nerd Lunch late this week to preview my INETA presentation next week. 

Sunday, January 01, 2006 9:41:19 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Tuesday, December 27, 2005
I have always had a small amount of spam on this site.  I checked my email on christmas day to discover I had 6000 referral spam instances (I had the blog engine set up to email me) waiting for me.  I will be temporarily turning off all the trackback, pingback, and referral features until I get time to upgrade the engine later this week.
Tuesday, December 27, 2005 10:16:16 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, December 20, 2005

As promised, I have some ideas regarding Async code for beginners.  Part one of three.

Compact Framework

The first thing to learn when doing asynchronous programming on the Compact Framework is that it is not as forgiving as the full .NET framework.  Threading things that might be a problem on a Pentium IV will be a problem on a 400mhz XScale.  The first problem you may run into, since its not well documented is Thread priorities.  You may want to give your "worker" threads more CPU time than the UI thread.  While the thread priority method exists on the thread class in the CF it is unofficially unsupported in CF 1.0.  I have tested this on at least 3 different devices with both CE.NET 4.2 and Windows Mobile and any attempt to set a thread priority locks up execution and debugging.

Your UI is running on a thread that processes events and updates UI controls.  In fact the compact framework starts quite a few threads you have no control over, for exampleTCP listeners and such when you execte web services.  Any attempt to access a GUI control's properties from outside this UI thread could cause unexpected behavior, including the program locking up and no longer responding.  Often you can get away with reading a control's properties without locking up, but setting a Label Text property is much more likely to cause a deadlock.  The way you access a control's properties from your worker threads, then, is using Control.Invoke.  The method description for Control.Invoke says:

"Executes the specified delegate on the thread that owns the control's underlying window handle."

That should be fairly straight forward.  There is a Message Pump running.  Every time something happens in GUI code that changes some visual aspect of a control it occurs as the Message Pump dispatches messages.  Code you call directly from the UI thread automatically gets executed this way,  Control.Invoke gives you a way to execute some code from within this message pump without exposing the internals to you.  On Compact Framework 1.0 only the version of Control.Invoke that takes a no-arguments Delegate is supported.  In CF 2.0, Control.Invoke (Delegate, object[]) is also supported, which eliminates the need for the following tip:

Since you cannot pass arguments to Control.Invoke in CF 1.0 you may need a way to communicate with your UI controls from a worker thread.  For example, suppose you have a custom sync process running on a worker thread and you want to update a Label each time a table is processed with the name of the next table name.  I would argue that the worker thread having any knowledge of the UI controls is bad, so you need a better means of communicating.  The Mediator/Director pattern from the GOF design patterns book provides a solution.  Consider the following:

On the Worker thread class:

OnPullStart(new PullStartEventArgs("Next Table Name"));

...

public event PullStartEventHandler PullStart;
        protected void OnPullStart(PullStartEventArgs e)
{
  if (this.PullStart != null)
  {
    PullStart(this, e);
  }
}

On the Director we have a method or property we can talk to which keeps the UI control decoupled from other classes:

...
public string SyncMessage
{
get{ return _syncMessage;}
set
{
_syncMessage = value;
_someControl.Invoke( new EventHandhler(_someControl.SyncMessageChanged));
}
}
...

On the GUI control owning the Label, which also kicked off the worker thread, you supply an EventHandler:

public void SyncMessageChanged(object sender, EventArgs e)
{
  _someLabel.Text = Director.Instance.SyncMessage;
}

Note that in some cases you may wish to synchronize access to the value from the Director.  Every application is different.  This is a somewhat simplified example.  In my real applications I typically have a "Context" (Possibly State pattern) object of some kind which keeps State for me, and the Director just fires messages.  There are different techniques to use, but I will offer some tips:

  1. Whenver dealing with updating the UI, always be concious of "What thread is this method being called from?"
  2. In light of #1, be sure ALL code that touches GUI components has come from an invoke method.  Sometimes even Querying a Control's property from outside the UI thread can cause a lockup.
  3. Use Events on a worker thread to signal a significant event that a client class may be interested in.
  4. Do not pass instances of your controls to worker threads so that the worker can call Invoke, use a technique that decouples the worker thread from any classes that might subscribe to the worker class's events.

Modal Mixing

The very excellent OpenNETCF libraries for the .Net compact framework provides some extended functionality for processing windows messages.  Much of this is in the ApplicationEx class, allowing you to run your GUI's main form using ApplicationEx.Run( new MyForm() );.  I have also experienced different GUI lockup situations where different ShowModal() and MessageBox calls are mixed.  If you need to show a modal Form be sure to use the new ApplicationEx.ShowDialog(Form, bool dispose) rather than Form.ShowDialog(); this will ensure the form is blocking the correct message pump.

The next threading article will be a short one discussing a couple of easy performance tricks you can use Threads for in ASP.NET.  In the article after that I will show some other threading techniques.

Tuesday, December 20, 2005 11:19:48 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Wednesday, December 14, 2005

.NET Architect, gourmet cook, and all-around good guy Matt Terski confided in me once that the great hurdle in getting peolpe to understand SOA was not teaching them transport protocols and such but getting them to Think Asynchronously.  I have posted before about how the Cage Builder culture in some organizations tells developers to "Never use threads".  This is a barrier to responsive apps, great performance, and adoption of SOA.

For my current client I built another custom data synchronization process for a mobile device.  We are not using Merge Replication for any number of reasons, mostly because of the constraints it puts on your database schema.  This sync process runs over a Sprint Airlink modem mounted on a truck; the speed of the modem is entirely dependent upon the quality of the connection it has at any given time.  Since the trucks could be in the middle of nowhere or surrounded by power lines I'm lucky to get 4k/s on some days.  A normal sync was taking about 3 and a half minutes and I was asked to optimize it.  We are moving to SQL Mobile in Q2 2006 but that doesn't help us today so I sat down to see what I could do to make this faster.

Long story short, I have it to just under one minute now.  They are pleased.

Most of the Performance talk you see going around deals with things like "avoid boxing", "use StringBuilder", "stored procedure", etc.  Those are the last things I look at, the first things are improving the efficiency of data stores and Asynchronous processing. 

Improving the efficiency of your database is a huge topic and one that I am only now becoming more familiar with.  My current sync process is largely based on DateLastChanged fields on various tables, and some of those are large for SQL CE, say 20,000 rows.  Adding an Index on the DateLastChanged fields of some large tables changed the operation from Table Scan to Bookmark seek, and took many seconds off of my times.  There is no execution plan viewer on SQL CE 2 to prove that statement, but the proof is in the performance. 

The larger part of my 300% improvement was "Processing Item A while waiting on Item B to be done". I am going to write three articles as I get time about some very simple Practices I use to keep me honest and reap the benefits of Thinking Asynchronously.  My 3-part intro to threading will consist of:

  1. Control.Invoke practices for the CF and Delegates in ASP.NET
  2. Practices for Work Item constructs
  3. Thread Signaling
Wednesday, December 14, 2005 11:27:57 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, November 28, 2005

Do not build with Cade Homes of Milwaukee!

Once upon a time I decided to build a house.  It hasn't been going well, and my builder could care less if they finish my house or if I drop dead tomorrow.  In my industry time and cost overruns happen often, however most Consulting firms suffer from this crazy idea that even when things go wrong and we make mistakes the customer should get good value from us and ultimately be happy not only with the end result of our work but the experience of working with us as well.  Maybe it has something to do with Milwaukee being a small town and people talking to each other...

In the case of my builder they can treat me however they want and still get their money, and they are obviously unconcerned with what unhappy customers say.  The only recourse I have is to encourage everyone who will listen to avoid building with this company.  Based on the rule that "each person tells 10 other people" perhaps some day they will track lagging sales back to that one customer that they made feel unimportant and crazy every chance they got.  Its far more likely that word will get around "Wow that Damon guy holdes grudges I guess" and for that reason I typically keep personal stuff off of this blog.

This post will remain until I get some incredibly intresting (ergo, don't hold your breath) piece of tech to post about.  If you are in the area and considering building a house, I'd be happy to send you copies of my two complaint letters to the Metro Milwaukee Builder's Association nicely summarzing Cade's apathy towards their customers.

Monday, November 28, 2005 1:47:21 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [3]  |  Trackback