Wednesday, November 4, 2009

Log Timmer

One common challenge in developing software is tuning performance. The application is running slow but you are not sure what is causing the problem. An approach to the debugging the problem is to use logging. This works, but you end up doing a lot of time arithmetic to determine how long it took some action to complete. Logging also adds many lines of code to the application muddling up the implementation and making it harder to read and understand. The solution is a class designed to time and log an event. I give credit to Scott Plichta for designing the architecture for the Log Timer.

Creating a log in C++ is easy. The constructor starts the timer and the destructor calculates the elapsed time and logs the results. This is a problem in Java. In Java, destructors are may not be called until the garbage collector runs. C# also uses garbage collection, but provides the IDisposable interface. IDisposable tells the garbage collector to dispose of the object as soon as possible. That means the timings provided by a log timer class in C# are not quite as accurate as the timing in a C++ log timer class.

How do you use the log timer class work in C#? The simplest method is just to place a "using" statement creating the log timer around the code you want to time:

using (new LogTimer.LogTimer())
{
// code you want to time
}

Call that is a little more complicated allows passing in the custom message:

using (new LogTimer.LogTimer("Deserialize time: "))
{
// code you want to time
}

The code above will always log. The LogTimer class has constructors that allow passing in the number of milliseconds for logging for each of the Log4Net levels. Below is an example passing in all the level thresholds:

using (new LogTimer.LogTimer("DownloadImage GetReponse time: ", 5000, 3000, 2000, 100, 0))
{
// code you want to time
}

There are several other constructors taking different number of thresholds.  Below is the implementation of the LogTimer class in C#.

using System;
using System.Collections.Generic;
using System.Text;

using log4net.Core;
using System.Diagnostics;

namespace LogTimer
{
// based on:
// http://www.eggheadcafe.com/articles/20050625.asp
// http://www.codeproject.com/KB/cs/idispose.aspx

public class LogTimer : IDisposable
{
/// <summary>
/// Create a logger for use in this class
/// </summary>
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(LogTimer));

/// <summary>
/// Used by the destructor to avoid disposing twice
/// </summary>
private bool disposed = false;

/// <summary>
/// Default message
/// </summary>
private string msg = "LogTimer: ";

/// <summary>
/// Threashold in milliseconds before issuing a debug log
/// </summary>
private readonly long debugCount = 0;

/// <summary>
/// Threashold in milliseconds before issuing a info log
/// </summary>
private readonly long infoCount = 0;

/// <summary>
/// Threashold in milliseconds before issuing a warn log
/// </summary>
private readonly long warnCount = 0;

/// <summary>
/// Threashold in milliseconds before issuing a error log
/// </summary>
private readonly long errorCount = 0;

/// <summary>
/// Threashold in milliseconds before issuing a fatal log
/// </summary>
private readonly long fatalCount = 0;

/// <summary>
/// Stopwatch to determine how long between starting and stopping
/// </summary>
private Stopwatch sw = new Stopwatch();


/// <summary>
/// Simple constructor that generates a default message for the current log level
/// </summary>
public LogTimer()
{
sw.Start();
}

/// <summary>
/// Simple constructor that generates the messages for the current log level
/// </summary>
/// <param name="message">The message to log</param>
public LogTimer(string message) : base()
{
msg = message;
}

/// <summary>
/// Most comprehensive constructor. Allows setting all the properties.
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="fatal">Threshold for fatal logs</param>
/// <param name="error">Threashold for error logs</param>
/// <param name="warn">Threashold for warn logs</param>
/// <param name="info">Threashold for info logs</param>
/// <param name="debug">Threadhold for debug logs</param>
public LogTimer(string message, long fatal, long error, long warn, long info, long debug) : base()
{
msg = message;
fatalCount = fatal;
errorCount = error;
warnCount = warn;
infoCount = info;
debugCount = debug;
}

/// <summary>
/// Construct allowing setting the message and threasholds for fatal, error, warn, and info.
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="fatal">Threshold for fatal logs</param>
/// <param name="error">Threashold for error logs</param>
/// <param name="warn">Threashold for warn logs</param>
/// <param name="info">Threashold for info and debug logs</param>
public LogTimer(string message, long fatal, long error, long warn, long info) : base()
{
msg = message;
fatalCount = fatal;
errorCount = error;
warnCount = warn;
infoCount = info;
debugCount = info;
}

/// <summary>
/// Construct allowing setting the message and threasholds for
/// fatal, error, and warn.
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="fatal">Threshold for fatal logs</param>
/// <param name="error">Threashold for error logs</param>
/// <param name="warn">Threashold for warn, info and debug logs</param>
public LogTimer(string message, long fatal, long error, long warn) : base()
{
msg = message;
fatalCount = fatal;
errorCount = error;
warnCount = warn;
infoCount = warn;
debugCount = warn;
}

/// <summary>
/// Construct allowing setting the message and threasholds for
/// fatal and error
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="fatal">Threshold for fatal logs</param>
/// <param name="error">Threashold for error, warn, info and debug logs</param>
public LogTimer(string message, long fatal, long error) : base()
{
msg = message;
fatalCount = fatal;
errorCount = error;
warnCount = error;
infoCount = error;
debugCount = error;
}

/// <summary>
/// Construct allowing setting the message and threashold
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="fatal">Threshold for fatal, error, warn, info,
/// and debug logs</param>
public LogTimer(string message, long fatal) : base()
{
msg = message;
fatalCount = fatal;
errorCount = fatal;
warnCount = fatal;
infoCount = fatal;
debugCount = fatal;
}

/// <summary>
/// Actual destructor
/// </summary>
~LogTimer()
{
Dispose(false);
}

/// <summary>
/// Dispose method
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Dispos method
/// </summary>
/// <param name="disposing">currently disposing</param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
sw.Stop();
long duration = sw.ElapsedMilliseconds;

// log with the highest enabled log level
// a log level is enable when IsLevelEnable and leveCount > 0
if (log.IsFatalEnabled && duration >= fatalCount)
{
log.Fatal(msg + duration.ToString());
}
else if (log.IsErrorEnabled && duration >= errorCount)
{
log.Error(msg + duration.ToString());
}
else if (log.IsWarnEnabled && duration >= warnCount)
{
log.Warn(msg + duration.ToString());
}
else if (log.IsInfoEnabled && duration >= infoCount)
{
log.Info(msg + duration.ToString());
}
else if (log.IsDebugEnabled && duration >= debugCount)
{
log.Debug(msg + duration.ToString());
}
}
}
disposed = true;
}

}
}

Monday, August 24, 2009

The Lost Weekend: IIS 7, PHP, and Zend

My plan for this weekend was to implement a new version of one of my web sites. I wanted to move from an old complicated build process to one based on using the Zend conventional modular layout. I estimated I could complete the task over the weekend. I was wrong.

Microsoft made a significant change by allowing IIS to run on the home versions of Vista. I could install IIS 7 on my new Vista 64 bit PC. I thought it would be a fun learning experience to use IIS 7 as a test bed while I implemented the changes. I was wrong.

Microsoft packaged up PHP and created an install using their new Web Platform Installer. This sounded like a quick way to get PHP up and running. If you want to get PHP up and running quickly on IIS 7, the Web Platform Installer will do that. However, if you want to use PEAR, the Web Platform Installer is a dead end. I have a problem with PEAR. I was unable to find any instructions on how to install PEAR manually. Everywhere I looked the documentation always said to run the "go-pear" script or batch file. Microsoft decided not to include that little file in their installation of PHP via the Web Platform Installer. When you install PHP via the Web Platform Installer on Vista 64 PHP is installed in the "\Program Files (x86)\php" directory. The installer then sets the include path to ".;c:\php5\pear" even though there is no php5 directory created. If you want PEAR and PHP on IIS, you want to avoid using the Web Platform Installer. Instead, use the "installer version" found at "windows.php.net". When you install, make sure you change the installation directory to "\php5". I also recommend you install PEAR, which is an option with the "installer version". I thought the Web Platform Installer quickly set up PHP and PEAR. I was wrong.

When you install IIS 7, it does not include a URL Rewrite Module. Only two things were quick and easy this weekend. The first was installing the IIS URL Rewrite Module and importing the rewrite rules from Apache. The second was downloading and installing the Zend Framework. The Zend Framework is a .Zip file you download, and decompress to anywhere you choose.

I used the Zend Framework before. I never started a project using MVC from scratch. I wanted to use the conventional modular layout to support three levels. There were tutorials that showed how to set this up. It looked very simple to me. I was wrong.

In previous entries, I mentioned "Practical Web 2.0 Applications with PHP" by Quentin Zervaas. Apress published the book in December of 2008. It is already out of date. About the time the book was published version 1.8 of the framework was released which deprecated the use of Zend_Loader's autoloader. I thought I could use the tutorial in the book by Zervaas to build a site from scratch. I was wrong.

I set up my site with a single controller. I kept getting the error "Invalid controller specified". I spent hours checking my directory structure. I googled the error message. Most web sites suggested that there was a problem with the file or directory casing. I am using IIS 7 on Vista. Files and directory casing should not be an issue. I thought I had some sort of typo in some file. I was wrong.

Finally, after much pain and cursing I found the problem. The problem is auto-rendering. The Zend framework automatically goes hunting for the template with auto-rendering enabled. Most of the sample code has a hidden assumption that auto-rendering is off. This is no longer true with the default configuration. I was getting the error "Invalid controller specified". The problem was not with the controller but with the view. Here is a situation where the MVC separation in Zend is poorly constructed. Zend generates an inappropriate error message.

Overall, I lost a weekend getting IIS 7, PHP, and Zend working. I did not finish redesigning my website. I still have a lot of work ahead. I was not expecting a major learning experience this weekend. I was wrong.

Saturday, July 25, 2009

Trust but Validate

The phrase "trust but verify" came out of the cold war. I learned SGML while working at ATLIS Systems. For many years ATLIS was a leader in SGML. One phrase coined by ATLIS was, "If a system is not based on a DTD and checked by an SGML parser, it is NOT SGML." In essence, you had to validate each and every instance. HTML (up through version 4) is not XML. HTML (up through version 4) is an SGML application. XML replaced SGML. I am amazed at how few XML aware applications never bother to create a DTD or Schema for the XML.

Years ago, I created a tool to help maintain my websites. The tool initially started out in Python. I really like the way the regular-expression library integrates with Python. Python refers to everything as an object. This allows putting function pointers into dictionaries and passing them to other functions. This was an important feature in the tool I created to maintain my websites. I created the user interface in Delphi. There is a mechanism to integrate Python and Delphi. I could quickly prototype the core regular expression replacements in Python and use that code in the finished user interface application.

I rewrote the Delphi code in C#. Although I thought the code was relatively clean, there were a few minor points that needed refactoring. I considered integrating my Python into C#. Instead, I found that the C# regular-expression library uses delegates. C# delegates are similar to function pointers in C/C++. Delegates allow you to pass function pointers to the regular-expression library just like you can do in Python. The C# syntax is a bit more wordy (verbosity never bothers me) than Python but a lot more strict. Python gives you enough rope to hang yourself by its lack of type checking. C# is a much strong typed language. I refactored my old Delphi and Python program into C#.

Microsoft added a lot of support for XML and HTML to .Net. One feature I did not have in the old version of my tool was a mechanism to validate the document. The code below shows how to validate a document in C#.

StringReader sr = new StringReader(WebPage);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationEventHandler += new ValidationEventHandler(PageValidationEvent);
XmlReader xvr = XmlReader.Create(sr, settings);
try
{
while (xvr.Read())
{
// do nothing
}
}
catch (XmlException e)
{
// handle any validation errors
}

The code below handles a single error.

public static void PageValidationEvent(object sender, ValidationEventArgs args)
{
// code to handle each error
}

Microsoft's validation is only mediocre. It catches most errors but not all. But for a few simple lines of code it is worth the effort.

I try to be very careful and ensure that my documents are correctly formed and valid. I started a major redesign of one of my websites. I originally built the site using my Delphi-Python tool. I had not done any major work on the site in years. When I started rebuilding the site using the C# version of my tool I found a lot of validation errors. Even though I tried to be careful I made some simple mistakes.

Those mistakes are the reason why I decided to write this article I titled "Trust but Validate". Now that I think about it, don't both with the trust, just validate your HTML and XML.

Tuesday, July 14, 2009

Developing using C# for Windows Mobile Suddenly Got Easier

I complained Microsoft did not make all the GDI and Win32 equivalent for C#. Well they are. Microsoft just didn't bother to make it easy to find. For me, it took a couple of days of searching before I stumbled on this great article: Optimize Your Pocket PC Development with the .NET Compact Framework. The title does not sound that important. This article gives you lots and lots of code you need if you want to develop using C# on Windows Mobile. For example, it provides a mapping of a large number of Win32 equivalent calls providing all the P/Involk code, constants, and enumerations. It also provides a Gdi class that maps a lot of the GDI calls. I wish I had found this article a couple of days ago.

The article does not describe everything included in the source code. It contains libraries Brushes, Fonts, Font Cache, GDI, Pens, the Registry, and Win32 calls. If you are developing for Windows Mobile and using C# you need to download the source code and look at all the hidden goodies.

Monday, July 13, 2009

Developing using C# for Windows Mobile is harder than it should be

I got side tracked today and starting playing with Quakk, a Twitter client for Windows Mobile. It was a good time to peak under the hood and learn how the Twitter API worked as well as see how someone implemented a Windows Mobile client. Quakk compiles and runs under Visual Studio 2008 once you get all the mobile development tools downloaded, installed, and configured. Microsoft makes that harder than it should. For example, in order to connect your mobile VM to the Internet you have to install Microsoft Virtual PC. Microsoft used to allow you download the network drivers. Now you have to install their entire Virtual PC in order to use the network drivers for the Windows Mobile VMs.

The Mobile VMs allow you to scale up the VM so it easier to read. Otherwise I would be stuck with 240x240 pixel emulation on a large screen. After you make the change you need to save the configuration or it will revert back. The same is true with the network settings.

Once I got Quakk running I took a good look at the code. Parts were well comments. Parts use lots of "magic numbers". For example, there were several places where the code would subtract 13 from the width of the panel. The default width of a vertical scroll bar is 13 pixels on Windows Mobile. At a bare minimum, that should have been a constant. It was not.

I decided to start removing the "magic numbers". This was a lot harder than it sounds. If you develop for Windows, then the System.Windows.Forms.SystemInformation class provides a great deal of information. Unfortunately, if you develop for Windows Mobile, Microsoft decided to lobotomize this class. This means for most applications you need to use the lower level GetSystemMetrics. This is fine if you are programming in C++. But if you are using C#, well things are harder than they should be. After much searching, I found the code to P/Involk the GetSystemMetrics call. Pinvolk.net is a useful resource, but as I write this, they did not document the Windows Mobile version of the GetSystemMetrics call; which is slightly different than the GetSystemMetrics call for Windows. Again, Microsoft made things a bit harder than they should be by changes the DLL that contains GetSystemMetrics when they ported it to Windows Mobile. My code for GetSystemMetrics is below.


using System;
using System.Runtime.InteropServices;

namespace WindowsAPI
{
public static class WinAPI
{
/// <summary>
/// Call the system metrics function
///
/// Windows mobile uses coredll.dll and not user32.dll
/// </summary>
/// <param name="smIndex">The value to return</param>
/// <returns>Integer value requested</returns>
[DllImport("coredll.dll")]
public static extern int GetSystemMetrics(SystemMetric smIndex);

/// <summary>
/// Windows mobile uses coredll.dll and not user32.dll
///
/// Flags used with the Windows API (User32.dll):GetSystemMetrics(SystemMetric smIndex)
///
/// This Enum and declaration signature was written by Gabriel T. Sharp
/// ai_productions@verizon.net or osirisgothra@hotmail.com
/// Obtained on pinvoke.net, please contribute your code to support the wiki!
/// </summary>
public enum SystemMetric : int
{
/// <summary>
/// Width of the screen of the primary display monitor, in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor, HORZRES).
/// </summary>
SM_CXSCREEN = 0,
/// <summary>
/// Height of the screen of the primary display monitor, in pixels. This is the same values obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor, VERTRES).
/// </summary>
SM_CYSCREEN = 1,
/// <summary>
/// Width of a horizontal scroll bar, in pixels.
/// </summary>
SM_CYVSCROLL = 2,
/// <summary>
/// Height of a horizontal scroll bar, in pixels.
/// </summary>
SM_CXVSCROLL = 3,
/// <summary>
/// Height of a caption area, in pixels.
/// </summary>
SM_CYCAPTION = 4,
/// <summary>
/// Width of a window border, in pixels. This is equivalent to the SM_CXEDGE value for windows with the 3-D look.
/// </summary>
SM_CXBORDER = 5,
/// <summary>
/// Height of a window border, in pixels. This is equivalent to the SM_CYEDGE value for windows with the 3-D look.
/// </summary>
SM_CYBORDER = 6,
/// <summary>
/// Thickness of the frame around the perimeter of a window that has a caption but is not sizable, in pixels. SM_CXFIXEDFRAME is the height of the horizontal border and SM_CYFIXEDFRAME is the width of the vertical border.
/// </summary>
SM_CXDLGFRAME = 7,
/// <summary>
/// Thickness of the frame around the perimeter of a window that has a caption but is not sizable, in pixels. SM_CXFIXEDFRAME is the height of the horizontal border and SM_CYFIXEDFRAME is the width of the vertical border.
/// </summary>
SM_CYDLGFRAME = 8,
/// <summary>
/// Height of the thumb box in a vertical scroll bar, in pixels
/// </summary>
SM_CYVTHUMB = 9,
/// <summary>
/// Width of the thumb box in a horizontal scroll bar, in pixels.
/// </summary>
SM_CXHTHUMB = 10,
/// <summary>
/// Default width of an icon, in pixels. The LoadIcon function can load only icons with the dimensions specified by SM_CXICON and SM_CYICON
/// </summary>
SM_CXICON = 11,
/// <summary>
/// Default height of an icon, in pixels. The LoadIcon function can load only icons with the dimensions SM_CXICON and SM_CYICON.
/// </summary>
SM_CYICON = 12,
/// <summary>
/// Width of a cursor, in pixels. The system cannot create cursors of other sizes.
/// </summary>
SM_CXCURSOR = 13,
/// <summary>
/// Height of a cursor, in pixels. The system cannot create cursors of other sizes.
/// </summary>
SM_CYCURSOR = 14,
/// <summary>
/// Height of a single-line menu bar, in pixels.
/// </summary>
SM_CYMENU = 15,
/// <summary>
/// Width of the client area for a full-screen window on the primary display monitor, in pixels. To get the coordinates of the portion of the screen not obscured by the system taskbar or by application desktop toolbars, call the SystemParametersInfo function with the SPI_GETWORKAREA value.
/// </summary>
SM_CXFULLSCREEN = 16,
/// <summary>
/// Height of the client area for a full-screen window on the primary display monitor, in pixels. To get the coordinates of the portion of the screen not obscured by the system taskbar or by application desktop toolbars, call the SystemParametersInfo function with the SPI_GETWORKAREA value.
/// </summary>
SM_CYFULLSCREEN = 17,
/// <summary>
/// For double byte character set versions of the system, this is the height of the Kanji window at the bottom of the screen, in pixels
/// </summary>
SM_CYKANJIWINDOW = 18,
/// <summary>
/// Nonzero if a mouse with a wheel is installed; zero otherwise
/// </summary>
SM_MOUSEWHEELPRESENT = 75,
/// <summary>
/// Height of the arrow bitmap on a vertical scroll bar, in pixels.
/// </summary>
SM_CYHSCROLL = 20,
/// <summary>
/// Width of the arrow bitmap on a horizontal scroll bar, in pixels.
/// </summary>
SM_CXHSCROLL = 21,
/// <summary>
/// Nonzero if the debug version of User.exe is installed; zero otherwise.
/// </summary>
SM_DEBUG = 22,
/// <summary>
/// Nonzero if the left and right mouse buttons are reversed; zero otherwise.
/// </summary>
SM_SWAPBUTTON = 23,
/// <summary>
/// Reserved for future use
/// </summary>
SM_RESERVED1 = 24,
/// <summary>
/// Reserved for future use
/// </summary>
SM_RESERVED2 = 25,
/// <summary>
/// Reserved for future use
/// </summary>
SM_RESERVED3 = 26,
/// <summary>
/// Reserved for future use
/// </summary>
SM_RESERVED4 = 27,
/// <summary>
/// Minimum width of a window, in pixels.
/// </summary>
SM_CXMIN = 28,
/// <summary>
/// Minimum height of a window, in pixels.
/// </summary>
SM_CYMIN = 29,
/// <summary>
/// Width of a button in a window's caption or title bar, in pixels.
/// </summary>
SM_CXSIZE = 30,
/// <summary>
/// Height of a button in a window's caption or title bar, in pixels.
/// </summary>
SM_CYSIZE = 31,
/// <summary>
/// Thickness of the sizing border around the perimeter of a window that can be resized, in pixels. SM_CXSIZEFRAME is the width of the horizontal border, and SM_CYSIZEFRAME is the height of the vertical border.
/// </summary>
SM_CXFRAME = 32,
/// <summary>
/// Thickness of the sizing border around the perimeter of a window that can be resized, in pixels. SM_CXSIZEFRAME is the width of the horizontal border, and SM_CYSIZEFRAME is the height of the vertical border.
/// </summary>
SM_CYFRAME = 33,
/// <summary>
/// Minimum tracking width of a window, in pixels. The user cannot drag the window frame to a size smaller than these dimensions. A window can override this value by processing the WM_GETMINMAXINFO message.
/// </summary>
SM_CXMINTRACK = 34,
/// <summary>
/// Minimum tracking height of a window, in pixels. The user cannot drag the window frame to a size smaller than these dimensions. A window can override this value by processing the WM_GETMINMAXINFO message
/// </summary>
SM_CYMINTRACK = 35,
/// <summary>
/// Width of the rectangle around the location of a first click in a double-click sequence, in pixels. The second click must occur within the rectangle defined by SM_CXDOUBLECLK and SM_CYDOUBLECLK for the system to consider the two clicks a double-click
/// </summary>
SM_CXDOUBLECLK = 36,
/// <summary>
/// Height of the rectangle around the location of a first click in a double-click sequence, in pixels. The second click must occur within the rectangle defined by SM_CXDOUBLECLK and SM_CYDOUBLECLK for the system to consider the two clicks a double-click. (The two clicks must also occur within a specified time.)
/// </summary>
SM_CYDOUBLECLK = 37,
/// <summary>
/// Width of a grid cell for items in large icon view, in pixels. Each item fits into a rectangle of size SM_CXICONSPACING by SM_CYICONSPACING when arranged. This value is always greater than or equal to SM_CXICON
/// </summary>
SM_CXICONSPACING = 38,
/// <summary>
/// Height of a grid cell for items in large icon view, in pixels. Each item fits into a rectangle of size SM_CXICONSPACING by SM_CYICONSPACING when arranged. This value is always greater than or equal to SM_CYICON.
/// </summary>
SM_CYICONSPACING = 39,
/// <summary>
/// Nonzero if drop-down menus are right-aligned with the corresponding menu-bar item; zero if the menus are left-aligned.
/// </summary>
SM_MENUDROPALIGNMENT = 40,
/// <summary>
/// Nonzero if the Microsoft Windows for Pen computing extensions are installed; zero otherwise.
/// </summary>
SM_PENWINDOWS = 41,
/// <summary>
/// Nonzero if User32.dll supports DBCS; zero otherwise. (WinMe/95/98): Unicode
/// </summary>
SM_DBCSENABLED = 42,
/// <summary>
/// Number of buttons on mouse, or zero if no mouse is installed.
/// </summary>
SM_CMOUSEBUTTONS = 43,
/// <summary>
/// Identical Values Changed After Windows NT 4.0
/// </summary>
SM_CXFIXEDFRAME = SM_CXDLGFRAME,
/// <summary>
/// Identical Values Changed After Windows NT 4.0
/// </summary>
SM_CYFIXEDFRAME = SM_CYDLGFRAME,
/// <summary>
/// Identical Values Changed After Windows NT 4.0
/// </summary>
SM_CXSIZEFRAME = SM_CXFRAME,
/// <summary>
/// Identical Values Changed After Windows NT 4.0
/// </summary>
SM_CYSIZEFRAME = SM_CYFRAME,
/// <summary>
/// Nonzero if security is present; zero otherwise.
/// </summary>
SM_SECURE = 44,
/// <summary>
/// Width of a 3-D border, in pixels. This is the 3-D counterpart of SM_CXBORDER
/// </summary>
SM_CXEDGE = 45,
/// <summary>
/// Height of a 3-D border, in pixels. This is the 3-D counterpart of SM_CYBORDER
/// </summary>
SM_CYEDGE = 46,
/// <summary>
/// Width of a grid cell for a minimized window, in pixels. Each minimized window fits into a rectangle this size when arranged. This value is always greater than or equal to SM_CXMINIMIZED.
/// </summary>
SM_CXMINSPACING = 47,
/// <summary>
/// Height of a grid cell for a minimized window, in pixels. Each minimized window fits into a rectangle this size when arranged. This value is always greater than or equal to SM_CYMINIMIZED.
/// </summary>
SM_CYMINSPACING = 48,
/// <summary>
/// Recommended width of a small icon, in pixels. Small icons typically appear in window captions and in small icon view
/// </summary>
SM_CXSMICON = 49,
/// <summary>
/// Recommended height of a small icon, in pixels. Small icons typically appear in window captions and in small icon view.
/// </summary>
SM_CYSMICON = 50,
/// <summary>
/// Height of a small caption, in pixels
/// </summary>
SM_CYSMCAPTION = 51,
/// <summary>
/// Width of small caption buttons, in pixels.
/// </summary>
SM_CXSMSIZE = 52,
/// <summary>
/// Height of small caption buttons, in pixels.
/// </summary>
SM_CYSMSIZE = 53,
/// <summary>
/// Width of menu bar buttons, such as the child window close button used in the multiple document interface, in pixels.
/// </summary>
SM_CXMENUSIZE = 54,
/// <summary>
/// Height of menu bar buttons, such as the child window close button used in the multiple document interface, in pixels.
/// </summary>
SM_CYMENUSIZE = 55,
/// <summary>
/// Flags specifying how the system arranged minimized windows
/// </summary>
SM_ARRANGE = 56,
/// <summary>
/// Width of a minimized window, in pixels.
/// </summary>
SM_CXMINIMIZED = 57,
/// <summary>
/// Height of a minimized window, in pixels.
/// </summary>
SM_CYMINIMIZED = 58,
/// <summary>
/// Default maximum width of a window that has a caption and sizing borders, in pixels. This metric refers to the entire desktop. The user cannot drag the window frame to a size larger than these dimensions. A window can override this value by processing the WM_GETMINMAXINFO message.
/// </summary>
SM_CXMAXTRACK = 59,
/// <summary>
/// Default maximum height of a window that has a caption and sizing borders, in pixels. This metric refers to the entire desktop. The user cannot drag the window frame to a size larger than these dimensions. A window can override this value by processing the WM_GETMINMAXINFO message.
/// </summary>
SM_CYMAXTRACK = 60,
/// <summary>
/// Default width, in pixels, of a maximized top-level window on the primary display monitor.
/// </summary>
SM_CXMAXIMIZED = 61,
/// <summary>
/// Default height, in pixels, of a maximized top-level window on the primary display monitor.
/// </summary>
SM_CYMAXIMIZED = 62,
/// <summary>
/// Least significant bit is set if a network is present; otherwise, it is cleared. The other bits are reserved for future use
/// </summary>
SM_NETWORK = 63,
/// <summary>
/// Value that specifies how the system was started: 0-normal, 1-failsafe, 2-failsafe /w net
/// </summary>
SM_CLEANBOOT = 67,
/// <summary>
/// Width of a rectangle centered on a drag point to allow for limited movement of the mouse pointer before a drag operation begins, in pixels.
/// </summary>
SM_CXDRAG = 68,
/// <summary>
/// Height of a rectangle centered on a drag point to allow for limited movement of the mouse pointer before a drag operation begins. This value is in pixels. It allows the user to click and release the mouse button easily without unintentionally starting a drag operation.
/// </summary>
SM_CYDRAG = 69,
/// <summary>
/// Nonzero if the user requires an application to present information visually in situations where it would otherwise present the information only in audible form; zero otherwise.
/// </summary>
SM_SHOWSOUNDS = 70,
/// <summary>
/// Width of the default menu check-mark bitmap, in pixels.
/// </summary>
SM_CXMENUCHECK = 71,
/// <summary>
/// Height of the default menu check-mark bitmap, in pixels.
/// </summary>
SM_CYMENUCHECK = 72,
/// <summary>
/// Nonzero if the computer has a low-end (slow) processor; zero otherwise
/// </summary>
SM_SLOWMACHINE = 73,
/// <summary>
/// Nonzero if the system is enabled for Hebrew and Arabic languages, zero if not.
/// </summary>
SM_MIDEASTENABLED = 74,
/// <summary>
/// Nonzero if a mouse is installed; zero otherwise. This value is rarely zero, because of support for virtual mice and because some systems detect the presence of the port instead of the presence of a mouse.
/// </summary>
SM_MOUSEPRESENT = 19,
/// <summary>
/// Windows 2000 (v5.0+) Coordinate of the top of the virtual screen
/// </summary>
SM_XVIRTUALSCREEN = 76,
/// <summary>
/// Windows 2000 (v5.0+) Coordinate of the left of the virtual screen
/// </summary>
SM_YVIRTUALSCREEN = 77,
/// <summary>
/// Windows 2000 (v5.0+) Width of the virtual screen
/// </summary>
SM_CXVIRTUALSCREEN = 78,
/// <summary>
/// Windows 2000 (v5.0+) Height of the virtual screen
/// </summary>
SM_CYVIRTUALSCREEN = 79,
/// <summary>
/// Number of display monitors on the desktop
/// </summary>
SM_CMONITORS = 80,
/// <summary>
/// Windows XP (v5.1+) Nonzero if all the display monitors have the same color format, zero otherwise. Note that two displays can have the same bit depth, but different color formats. For example, the red, green, and blue pixels can be encoded with different numbers of bits, or those bits can be located in different places in a pixel's color value.
/// </summary>
SM_SAMEDISPLAYFORMAT = 81,
/// <summary>
/// Windows XP (v5.1+) Nonzero if Input Method Manager/Input Method Editor features are enabled; zero otherwise
/// </summary>
SM_IMMENABLED = 82,
/// <summary>
/// Windows XP (v5.1+) Width of the left and right edges of the focus rectangle drawn by DrawFocusRect. This value is in pixels.
/// </summary>
SM_CXFOCUSBORDER = 83,
/// <summary>
/// Windows XP (v5.1+) Height of the top and bottom edges of the focus rectangle drawn by DrawFocusRect. This value is in pixels.
/// </summary>
SM_CYFOCUSBORDER = 84,
/// <summary>
/// Nonzero if the current operating system is the Windows XP Tablet PC edition, zero if not.
/// </summary>
SM_TABLETPC = 86,
/// <summary>
/// Nonzero if the current operating system is the Windows XP, Media Center Edition, zero if not.
/// </summary>
SM_MEDIACENTER = 87,
/// <summary>
/// Metrics Other
/// </summary>
SM_CMETRICS_OTHER = 76,
/// <summary>
/// Metrics Windows 2000
/// </summary>
SM_CMETRICS_2000 = 83,
/// <summary>
/// Metrics Windows NT
/// </summary>
SM_CMETRICS_NT = 88,
/// <summary>
/// Windows XP (v5.1+) This system metric is used in a Terminal Services environment. If the calling process is associated with a Terminal Services client session, the return value is nonzero. If the calling process is associated with the Terminal Server console session, the return value is zero. The console session is not necessarily the physical console - see WTSGetActiveConsoleSessionId for more information.
/// </summary>
SM_REMOTESESSION = 0x1000,
/// <summary>
/// Windows XP (v5.1+) Nonzero if the current session is shutting down; zero otherwise
/// </summary>
SM_SHUTTINGDOWN = 0x2000,
/// <summary>
/// Windows XP (v5.1+) This system metric is used in a Terminal Services environment. Its value is nonzero if the current session is remotely controlled; zero otherwise
/// </summary>
SM_REMOTECONTROL = 0x2001,
}
}
}

Now I was able to remove one set of magic numbers from Quakk. I run Quakk on an old Treo 700w. The Treo has a 240x240 screen. If you tweet using Quakk on a 240x240 device the system displays a horizontal scroll box. Quakk made some incorrect assumptions about screen size. I was able to fix the horizontal scroll bar problem with some minor code changes. In that same code, Quakk makes some assumptions about the font size and placement of the input boxes. The correct way to fix that is to call the SHGetUIMetrics to get the current system font size. SHGetUIMetrics is not available without using a P/Involk. I found one article with the sample code.

I thought I would make a few changes. I did, but it was a lot harder than it should have been.


Sunday, June 7, 2009

Technical Challenge of Multiple CPUs

One of the biggest technical challenges in developing software is taking advantage of multiple CPUs. Processor speed has basically stalled. Intel and AMD are putting more and more CPUs into the same chip. Right now dual and quad core chips are becoming common. If you are talking two years away, then by Moore's Law, you will be seeing eight or more cores in most systems. If it is three years away, you could be seeing sixteen or more cores.

Currently, only very high end systems have multiple GPUs. We had one system at a company I used to work for that had four GPUs (all very high end stuff doing medical imaging). But just like CPUs, multiple GPUs are going to be more and more common.

The challenge of writing code that supports multiple processors working in parallel is the real challenge. Some argue that current computer languages do not support that well. In my experience, threads (which are supposed to be light weight processes) do not scale well on multi-core CPU systems. We had a Java application with six main threads running on a system with two dual core chips (total of 4 cores) and we saw almost no performance gain for that application over running on a single core CPU. The system performed better because the system basically gave one CPU to the SQL server and another to the OS, but our multi-threaded application did not get any faster. You could blame that on Java, but putting the blame there does not solve the problem. Also we had similar problems with a multi-threaded application written in C++. The current state of compiler and operating system support for multiple CPUs is limited.

Right now I am drooling over the new Alienware M17x. You can get a laptop with a quad core CPU and dual GPUs. That would be a good replacement for aging Alienware laptop. It has been a great system, but it is getting old.

I hope that the operating system and programs will take full advantage of the 16 CPUs and 8 GPUs I hope to have in the computer I hope to have in a couple of years.

First Impression of Windows Mobile 6.5 Developer Tool Kit

I downloaded and installed the Windows Mobile 6.5 Developer Tool Kit. My first impression is yawn. I am putting a lot of emphasis on Windows Mobile but I found the 6.5 developer tool kit to be a big disappointment. The tool kit includes two new features: gesture API and widgets.

Personally, I think the gesture API is a yawn. I don't mind using a stylus. I have a nice set of pens that can print in black, red, pencil, or act as a stylus. People don't complain about using a pen on a piece of paper; why do they complain about using a stylus on a device? With a multi-function tool that can act as a pencil, pen, or stylus, I don't have to put the tool down to use a phone or PDA. I will have to play with the gesture API, but at this point it is just a yawn.

My first thought about widgets was so what? My second thought was about the same. The only reason widgets are at all interesting is they point out the capabilities of the new browser included on Windows Mobile 6.5. It is too early for me to tell, but that may be a reason alone to upgrade to 6.5. The only thing that worries me is the example widget posted by Microsoft does not use cascading style sheets or JavaScript. The "hello world" example makes me worry that the support for CSS and JavaScript may still be a little lacking. I will wait until someone else spends the time and effort documenting what works and what does not before do anything serious with widgets. One interesting thing about widgets is Microsoft actually points to the W3C standard for Widgets. A Google search about Widgets pulls out this gem: Apple threatens to block W3C widget standard. I am not worried that Apple's attempt to block the W3C will have any impact on Microsoft. After all, Microsoft will do what it has always done, create a non-standard way. But this time Microsoft could say, "Don't blame us, Apple forced us to."

The 6.5 tool kit screams, "I was pushed out quickly!" A lot of documentation about 6.5 on the MSDN site is missing. I found several broken links just trying to find out what was new. Another thing that reinforces that is the DRK or Developer Resource Kit will not be available until July. Microsoft is going to charge $9.99 for the DRK. I was going to order a copy. However the MSDN order site asked for my credit card without using a secure web page. The page does not display the little lock icon in IE8. This is probably another bug caused by pushing out the release. Until Microsoft fixes this, I will not put my credit card information into an unsecure web page. I discourage anyone from ordering the DRK until you can order it from a secure web page.

Unless you are hot for the gesture API or want to explore the capabilities of the new browser there is little need to download the Windows Mobile 6.5 Developer Tool Kit.

Friday, June 5, 2009

Converting Campaign Cartographer Files to XAML Revisited

I have spent a lot of time working with Campaign Cartographer 3, Adobe Illustrator CS, Corel Draw 12, and Expression Blend 3. I finally came up with a solution on how to create a XAML file from Campaign Cartographer 3. CC3 will export a Windows Metafile (.emf). Adobe Illustrator CS sometimes has problems with these files. The solution is to open the .emf file in Corel Draw and save the result as an Adobe Illustrator file. This also offers the opportunity to convert all the text to curves. Adobe is very proprietary about fonts. Converting text to curves fools Adobe Illustrator and you have no more issues with fonts, at the cost of a bigger file.

After saving the file as an Adobe Illustrator file from Corel Draw you need to load the file into Adobe Illustrator and save it out again. Expression Blend 3 will not load the files created by Corel Draw. Adobe Illustrator adds "[converted]" to the file name when it saves the file.

Now you can load the converted Adobe Illustrator file directly into Expression Blend. You may need to do a few transformations to move and scale the map but it works.

If you export something small from CC3, then you must scale it up in Corel Draw. Full maps work without the need to scale it up.

Thursday, June 4, 2009

Why Windows Mobile?

One of the questions I always get when I present the business concept for JSM Software is "why did you choose Windows Mobile?" Some are even less subtle and ask, "Why not develop for the iPhone instead?" They usually follow up with the story about how a friend of a friend made a lot of money selling some application on the iPhone.

Anecdotal evidence is not a good reason to start a company. I know a friend of a friend that won the lottery. It makes as much sense spending all my time, effort, and money trying to win the lottery as it does trying to make a lot of money creating applications for the iPhone. Both have the same anecdotal evidence.

I am trying to build a company, not win the lottery. I worked for a company that tried to "win the lottery". They came up with wonderful ideas that would change the entire company. None of it ever came to fruition. I want to emulate the conservative playbook of Woody Hayes, "three yards and a cloud of dust". Another description was "crunching, frontal assault of muscle against muscle, bone upon bone, will against will." Paraphrasing Woody Hayes, "a company will not succeed unless they regarded the challenge positively and would agree to pay the price that success demands of a team."

My business philosophy is simple: I want to grow a dedicated customer base by continually creating quality products. Sure I would like to make a lot of money. Everyone does. I will only focus on a market I know will provide adequate compensation for my effort.

One reviewer wrote me, "The iPhone market is so oversaturated with bad games, it's unbelievable." The vast number of cheep bad games put tremendous downward pricing pressure. Rick Storm wrote in his blog The Incredible App Store Hype, "The hype surrounding the iPhone App Store continues to persist." Rick Storm has an application is listed as #34 on the social networking chart. According to his blog that makes him a little over $20 per day. That is a little over $7000 per year if sales continue at that rate. That is not a lot of money to me.

Owen Goss documented his success with his game Dapple on his blog. Owen Gloss wrote about developing applications for the iPhone, "...I suspect we'll see fewer and fewer stories about people getting suddenly very rich." (The Numbers Post (aka Brutal Honesty)). Owen Gloss spent $32,000 developing Dapple and made a total of $535 in revenue. Playing the lottery sounds like it has a lot better odds than that. I fully agree with Owen Gloss, "My hope is that we'll start seeing more developers putting out quality titles in the hopes of gradually growing a sustainable business." Owen Gloss strongly implies developing games for the iPhone is not currently a sustainable business.

This reminds me of a stanza from one of my favorite poems, "The Gods of the Copybook Headings" by Rudyard Kipling:

With the Hopes that our World is built on they were utterly out of touch.
They denied that the Moon was Stilton; they denied she was even Dutch.
They denied that Wishes were Horses; they denied that a Pig had Wings.
So we worshiped the Gods of the Market Who promised these beautiful things.

I hear a lot of people worshiping the Gods of the Market in the form of praising the iPhone App Store.

Before deciding on Windows Mobile I did a lot of research. This is something it appears many jumping into the fray of developing software for mobile devices do not do. Denise Marcus, a friend of mine, always said, "Do the numbers." I have always been amazed at how many people fail to do the numbers.

What are the numbers? According to wmpoweruser.com (Windows Mobile hits 50 million lifetime sales ) Microsoft has sold over 50 million Windows Mobile devices. Of that, approximately 30 million are currently in use. These numbers make sense. According to Gartner, Microsoft has been selling over 4 million windows mobile devices every quarter since 2007 (Gartner Says Worldwide Smartphone Sales Reached Its Lowest Growth Rate). Most mobile contracts are two years long. 4 million per quarter times 4 quarters per year times 2 years equals 32 million. The 30 million number may be a bit low, but it is a reasonable starting point.

According to Entertainment Software Association 36% report they play games on a wireless device such as a cell phone or PDA (Essential Facts About the Computer and Video Game Industry). That means that 10.8 million Windows Mobile Users play games. The Entertainment Software Association also said that 7.6% of people who play games play role playing games. Conservatively the market for the type of games created by JSM Software is eight hundred and twenty-one thousand (821,000). If JSM Software reaches a one half of one percent market penetration, 0.5%, the sales projection is four thousand one hundred units (4,100).

My budget for creating a game is about $20,000. That means I need to make about $5 per unit sold. That does not include all the overhead costs in addition to the $20,000 budget. The interesting point is the average price of a role playing game for Windows Mobile on Handango is about $20. After crunching the numbers and using the wonderful spreadsheet provided by SCORE I determined I could make a living if I sold the game for $15 and sold a total of about 4,100 units over the lifespan of the game which I estimated to be one year. I bounced these numbers off of the CEOs of two other game companies. One CEO said my figures were "spot on".

Owen Goss said he needed to sell 9,150 units of Dapple before he would break even. I leave it to the reader to figure out what percentage of the market Dapple would require to break even. I am pretty sure it would be much larger than 0.5%.

The thing that puzzles me the most is why some people are so adamant I should develop for the iPhone? It seems they are trying to evangelize me to the iPhone religion. They believe all the hype and anecdotal stories. My choice to develop for Windows Mobile is an informed decision based on research. I am not embarking on creating a company based on anecdotal stories.

Sunday, May 24, 2009

Problems with Practical Web 2.0 Applications with PHP

I wanted to use the code from Quentin Zervaas's book for a website. There is a lot of good material in the book. Unfortunately, the development environment used by Quentin Zervaas must be very different from my development environment.

I could not get the code to work for days. I kept fumbling around and fumbling around trying to figure out what was wrong. The first problem was the index page code was crashing and leaving no log. It took me a while, but the problem was with the EmailLogger code bye Zervaas.

I am running on a Fedora 10 Linux development server. This is a minimally configured virtual machine running on my under powered VM server. This is not a production server. The first problem comes from the minimal configuration. Zervaas wrote the following code in the file "index.php":

$writer = new EmailLogger($_SERVER['SERVER_ADMIN']);

The code is simple enough it passes the "SERVER_ADMIN" name to the EmailLogger function. On a minimally configured system the value of "SERVER_ADMIN" is "root@localhost".

The EmailLogger function calls Zend_Validate_EmailAddress to make sure the email address is valid. The code is shown below:

$validator = new Zend_Validate_EmailAddress();

The default constructor for the Zend_Validate_EmailAddress only allows DNS hostnames. This means that "root@localhost" is not a valid email address using the default parameters when creating Zend_Validate_EmailAddress. This took a long time to figure out.

The solution is to tell the constructor to allow both DNS and local names. The code to do that is below:

$validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_LOCAL);

The support site for "Practical Web 2.0 Applications with PHP" does not document the problem with the email validation. The support site did document the next problem I ran into. This was extra slashes at the beginning of each URL. Instead of the URL being "/index" it was "//index". I tracked down the problem quickly. But every solution that made sense to me failed. The support site has two lists of errata for the book. One list is the old and unmaintained list. The other list is the new and maintained list. The new list does not contain the errata listed on the old list.

A more subtle problem is the wording of the errata. The old and unmaintained list of errata states, "When using Zend Framework 1.0.3 URLs will be generated beginning with 2 slashes." What the errata should have said is "When using Zend Framework 1.0.3 or newer URLs will be generated beginning with 2 slashes." I installed the Zend Framework 1.7.8.

I now have a problem with Captcha and image generation. This has been yet another learning experience.

Monday, May 4, 2009

Rounding Error Are an Important Consideration

My background includes a lot of mathematical analysis. I took a lot of numerical analysis at the University of Utah. Many more classes than were actually required because I enjoyed the combination of math and programming. In addition, I was a teaching assistant for the computational physics classes taught as part of the master's of instrumentation program of the physics department. My first job when I left the University of Utah was working on mathematical educational software. My assignment was part of a group creating a system that symbolically solved mathematical problems. I learned a lot. One of the challenges was checking the work of the system. I spent months doing nothing but math. After giving the system a problem it would generate an answer. Often the answer didn't match the solution printed in the book. We had to determine if the computer's answer was actually the correct answer. The system may not have simplified the answer, or not done some trigonometric substitution or maybe the answer in the book was wrong. It was a lot of work. That taught me the importance of mathematical simplification.

Years ago I worked on a respiratory therapy patient demographic and therapy management system. The company's core intellectual property was the set of equations modeling the treatment efficacy. The equations took the raw numbers provided by the medical instruments and the patient demography and created a meaningful human understandable diagnosis. My task was implementing those equations on a new system. I took one look at the equations and knew something was wrong with them. My background in numerical analysis told me curve fitting software created the equations. The curve fitting software mathematically matched the curve but did not simplify the equations. I convinced the professor that created the equations I could simplify them. For the next month I proved symbolically and numerically my simplification of the equations was correct. Imagine working on a month long math test. I proved the error analysis of my equations was significantly smaller than the original equations. More importantly, the resulting program ran more than ten times as fast.

What does all that have to do with XAML, hex grids, and user controls? You would think I would know better. I start working on making a hexagon a user control and what bites me? Rounding errors! I should have known that rounding errors would cause problems.

I started working on converting the shaded hexagon from my previous blog into a user control. I needed to work out the coordinates where to put each hexagon to create a hex grid. I created a column of hexagons. They looked good. I then added a second column, a third, and a fourth. Things looked great as you can see below.

Then I zoomed in a bit. Then I noticed that the hexagons did not always touch. I immediately knew the problem: rounding errors. You can see that below.

Hexagons tile the plane. Unfortunately you cannot tile them in XAML because you can only tile using simple rectangular patterns. I could use more precision. That would make the problem less likely to appear. The other option is just to make the hexagon grid as a series of polygons; each one calculated using as much accuracy as possible. You can see that below.

It looks like all is well with the hex grid. Until I made a simple change: I changed the stroke width to zero. You can see the result below.

Rounding errors are still visible. What does that mean? The short answer is you cannot expect to perfectly replicate a hexagon grid.

Sunday, May 3, 2009

No Geometry Resources in Silverlight 2

I wanted to create a hexagon grid. There is a good article on how to draw a hexagon grid using C# code here.

I wanted to create my grid in XAML. I used Excel to do the calculations. I also used Excel to generate the polygon points statement. The simplest hexagon in XAML I could come up is coded like this:

<Canvas
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="480"
    Height="480"
>
    <Polygon Stroke="#000000" StrokeThickness="1" Points="0,35,20,0,60,0,80,35,60,69,20,69"/>
</Canvas>

That looks like this:

My next step was to set up a nice radial fill. I put the fill as a static resource. The resulting code looks like this:

<Canvas
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="480"
    Height="480"
>

    <Canvas.Resources>
        <RadialGradientBrush 
            x:Key="HexRadialFill"
            GradientOrigin="0.5,0.5" 
            Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
            <RadialGradientBrush.GradientStops>
                <GradientStop Color="LimeGreen" Offset="0" />
                <GradientStop Color="Green" Offset="1" />
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
    </Canvas.Resources>

    <Polygon Stroke="#000000" StrokeThickness="1" Fill="{StaticResource HexRadialFill}"
        Points="0,35,20,0,60,0,80,35,60,69,20,69"/>
</Canvas>

The resulting image looks like:

I now planned on making the polygon a resource. Unfortunately you can't do that in Silverlight 2. I looked into converting the polygon into a path and then putting that as a geometry resource. Alas, Silverlight 2 does not allow geometry resources.

I have a couple of options. I could make the hex a user or custom control. That would allow code reuse. But at this time, I just want a hex grid. The quick and dirty way is to just write some more macros in Excel and generate the entire grid as a bunch of polygon statements. I could do that probably quicker than it took me to write this blog. However, I am dedicated to quality. I would never allow a developer who worked for me to hack out something quick and dirty like that. That means I need to create a user control and code that up. It will take me longer to code up the user control. The advantage is the resulting code will be reusable (even though I doubt I will reuse it).

I now know what it was like to work for me. I am somewhat of a demanding manager. I want things done the right way with no quick and dirty solutions. Now I can say I apply that standard even to my own work.

Thursday, April 23, 2009

In Praise of Multi-Boot

I use a tool named System Commander to allow multi-booting. I recently ran into an interesting reason why multi-booting is very useful. I have heard comments that virtual machines make multi-booting almost unnecessary. I disagree. I have a PC that I upgraded numerous times. It started out as a 486 to give you an idea of how long I have had this. I don’t think anything is left of the original Gateway 486. As disk drives became less and less expensive I copied the images of the operating system over to new drives. I configured the system with three separate operating systems: Windows 2000, Windows XP, and Windows Server 2003. Using System Command the computer normally boots up into Windows XP but I have the option of booting up into old Windows 2000 or into Windows Server 2003. On a side note, when I boot up Windows 2003 I then run VMware server and have several VMs. Sometime last summer, the sound out of the system started to act a little strange. There were pops, clicks, and stutters when playing music on Windows XP. I believe two different things causes issues with the sound system. First the sound card may have started to die. Second, one of Window XP innumerable updates may have cause the problem. True to form, the sound completely died. I swapped out the sound card with another slightly different one. After reinstalling the drivers on all three operating systems, I have great sound on two of them: Windows 2000 and Windows Server 2003. But Windows XP still has pops, clicks, and stutters. This means the problem is not with the sound card, but with Windows XP. It does not solve the problem, but it eliminates one possibility. Besides being able to switch operating systems, System Commander has the ability to repartition many disk formats. It is interesting that only Windows XP is affected. I considered installing Windows Vista. But I did not have the disk space.

Monday, April 20, 2009

Converting Campaign Cartographer Files to XAML

ProFantasy created a program named Campaign Cartographer. The program allows the quick creation of very high quality maps for role playing type of games. I was working on a logo for the company I am trying to start. I wanted to create a Silverlight XAML file for the logo. I knew that Campaign Cartographer could quickly and easily create a piece of the Logo. Now for the challenges in getting that file into XAML. Campaign Cartographer has only a limited number of vector output options. I could easily create a bitmapped graphic in a variety of formats, but I wanted to stay in the vector space. The logical place to start is as a Windows Metafile (.wmf or .emf). I exported the metafile and tried using one of the freeway tools EMFtoXAML. This produces basically produces an empty file. EMFtoXAML does not support any of the drawing commands used by Campaign Cartographer. I then tried reading the .emf file into Adobe Illustrator CS 1 (the most recent version I own). The result is very low quality. My next step was Corel Draw 12 (the most recent version I own). Again, the result was awful. I gave up on .emf files. Now to try another vector format: AutoCad .dxf. Now the fun begins. The current release of Campaign Cartographer is version 3. On my system, Campaign Cartographer 3 crashes when trying to export a .dxf. I then fire up an old PC that has Campaign Cartographer version 2 installed. This program exported the DXF without problem. Then went back to Adobe Illustrator CS 1 to try and import the .dxf file. Again, Adobe Illustrator did not render the import usable. My next step was back to Corel Draw 12. Corel Draw 12 imports the .dxf. The result of the import into Corel Draw is not perfect. The import removes most but not all of the color. At least the outline is clean. Corel Draw can export an Adobe Illustrator file or .ai file. I installed the preview version of Expression Blend 3.0 from Microsoft. Expression Blend 3.0 is supposed to be able to import Adobe Illustrator files and create XAML. I fired up Expression Blend and created a Silverlight project. Expression Blend disables the menu options to import an Adobe Illustrator file when you create a Silverlight project. After fumbling around for a while I tried creating a WPF project in Expression Blend. That allows importing Adobe Illustrator files. I did some searching on the web, and nothing mentions you can only import Adobe Illustrator into WPF projects. I tried importing the Adobe Illustrator file created by Corel Draw 12. Expression Blend 3.0 Preview says it cannot understand the file and cannot import it. I then go back to Adobe Illustrator. I open up the file created by Corel Draw in Adobe Illustrator and save the file back as another .ai file. It is interesting to note the Adobe Illustrator's default name for the file adds the word "[Converted]" to the file name. I open Expression Blend 3.0 back up to my WPF project. Finally I can import the .ai file and the image appears in Expression Blend. The colors are gone, but at least I have the XAML file I can work with. I hope that someday ProFantasy will add the ability to export data directly into XAML.

Wednesday, April 8, 2009

Windows Forms versus Web-Based Technology

A friend asked a question: "I have a question...curious about what you know about web-based system architecture design. Specifically, use of Windows Forms (.net framework) versus Frames Technology." The short answer to your question is web-based systems run inside a browser, and windows forms based applications run as applications on your PC. But things are a bit more complicated. Web-based systems use a variety of standards: HTML, XHTML, XML, and CSS. Developing a web based application requires testing on a variety of browsers (Internet Explorer, FireFox, etc.). You can access a web-based systems can from mobile devices like a BlackBerry, iPhone, and so on. Because they run in a browser, web based application also run on Macs and other operating systems besides Windows. In general, web based systems do not need to be installed on the computer. To run a web based application you just open up the browser and point it to the URL for the application. The downside is that Web-based systems have limitations on what data they can store or access on your local computer. A well designed web based application allows changing the "skin" of the application easily. Someone can change the colors, fonts, or layout used in an application without the need to recompile or reinstall the entire program. You must have a connection to the network for a web-based application to run. The phrase "software-as-a-service" or SAAS is usually associated with web based applications. Web-based applications run from a server. Changing the application on the server changes the application for every user at the same time. Windows forms based applications only run on a PC. You must install windows forms based applications. This means the application can take full advantage of any hardware you have on your PC. The application can access and store data on your local hard drive. To change the fonts, colors, or layout of the application usually required a new build of the application and a new install. (There were tools to support "skins".) Things are ever more complicated. Technology continues to innovate. Windows forms technology is old. Microsoft replaced Windows forms with Windows Presentation Foundation or WPF. Another name for this technology is Silverlight. WPF makes it easy to change the graphics, layout, fonts, and colors used by an application. This technology allows developers to use a component based development methodology. Developers just click and drop the components they need on the form and connect them. Another name for component based development is rapid application development or RAD. Customization of the application to make it look pretty no longer requires a developer. By the way, this was a huge issue at one company I worked for. I remember one project where the developer finished the functionality of the application in about two weeks. Then the developer spent the next nine months continually changing the colors, fonts, and layout to make the application pretty enough. (I used to get into nasty arguments about the time wasted on changing the colors, fonts, and layout.) Silverlight applications run inside most browsers. The server (or servers) must be up and running twenty four hours a day, seven days a week, three hundred and sixty five days a year for Web-based or Software-as-a-service based applications. If the server or network goes down, everyone is down. You install windows based applications on each PC. If the PC is working the application usually works. However, if the application requires access to a database, then the application only works if the database server is up and running. The biggest headache to windows based applications is the release of new versions of the software. Technology continues to change. Perhaps the best example is the game World of Warcraft. This is an application you install on your local PC that talks to a set of servers. World of Warcraft automatically installs new versions when you log in. The game won’t run if you don’t have an Internet connection. World of Warcraft uses your graphics card to the max. Performance of the game is generally limited by your PC and not by the server. This does put a bit of a strain on some network connections. If you need to make the decision on which technology the first question is: "will the individual computers always have a connection to the network and/or Internet?" If the answer is "no", then in general you want to stay away from a web-based solution. (I did manage a project that embedded a web server inside the application, but that is a complicated solution.) If the answer to that question is "yes", then you need to think about the type of computers each person who runs the application has. If you have a combination of PCs and Macs, then you should go with a web-based solution. If one of the stakeholders places the esthetics of the application very high, then go with a web-based solution. If you plan on changing the application frequently, it is easier to change a web-based solution. On the other hand, if you need to get performance out of the local PC, go with a windows based solution.

Saturday, January 31, 2009

Lack of Pride in your Work

I recently downloaded a small program from an open source repository. I took a look at the sources and was shocked. With the exception of some commented out code, the IDE generated all the comments. There was not even a header on the files showing that the source code was under an open source license or giving credit to the original author. I have experience working with poorly written and/or poorly commented code. I admit that I have written some code like that. But I also have written some code that is easy to maintain and is well commented. Early in my career I learned from some excellent teachers. I still have some of the papers that Nelson Beebe wrote about writing portable software almost thirty years ago. Brad Davis taught me a lot when I worked at the University of Utah. I still have some code I wrote way back when that still runs. The code I wrote then did not have a lot of comments, but the naming conventions and structure make it easy to read and understand. If I wrote that code today, I would use some different approaches, but then programming has changed a lot over the last twenty to thirty years. I know the quality of my twenty year old code is better than the source code to the small program I downloaded. Part of the idea of open source is pride in your work. How can a developer lack the pride to polish their code before they publish it to an open source repository? If you publish your code you expect others to read your code. Don't you clean up your home before you invite visitors in? You should do the same thing with your code. Clean and polish your code before you publishing it. Take pride in doing a good job. There is a saying, "a job worth doing is worth doing well." One of the things I require of the people that work for me is that they do a good job that they can be proud of. I know a lot of companies rush to get software out the door. This is particularly true in startups. Get it working is the mantra. But that hurts you in the end. You create products that are not maintainable. I establish the processes and procedure to take an immature software organization and create a predictable, sustainable, measurable, and managed team that delivers quality products on time and on budget.