Posts


My-FyiReporting 4.5.0 has been released.

Main new features:

  • Gtk Viewer based on cairo
  • Barcodes QR Codes, Code39, EAN8
  • Menu Insert* Upgraded to visual studio 2010
  • Helper to select SQLite database
  • Helper to select SQL Server 2005/2008 server and database
  • Enable drag and drop of reports into designer when designer had no other reports open
  • Wpf report viewer control

BUG fixes: Issue 12 - Aggregate Function First() returns data from wrong dataset Issue 8 - Field place holders in Expression builder Issue 7 - Setting Paper Size Issue 13 - Print multiple copies not working

Download source code packages from https://github.com/majorsilence/My-FyiReporting/tags and binary packages from https://github.com/majorsilence/My-FyiReporting/downloads.


To create a QR code using C# or any .net language first go to http://code.google.com/p/zxing/ and download the C# library and build it. Direct link for version 2 source (http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.0.zip&can=2&q=)

Add the zxing.dll file as a reference to your project.

        using System.Drawing;
        Bitmap bmp = QRCode("The Message To Encode");

        public Bitmap QRCode(string codeMessage)
        {
            com.google.zxing.qrcode.QRCodeWriter writer = new com.google.zxing.qrcode.QRCodeWriter();
            com.google.zxing.common.ByteMatrix matrix;

            int size = 180;
            matrix = writer.encode(codeMessage, com.google.zxing.BarcodeFormat.QR_CODE, size, size, null);


            Bitmap img = new Bitmap(size, size);
            Color Color = Color.FromArgb(0, 0, 0);

            for (int y = 0; y < matrix.Height; ++y)
            {
                for (int x = 0; x < matrix.Width; ++x)
                {
                    Color pixelColor = img.GetPixel(x, y);

                    //Find the colour of the dot
                    if (matrix.get_Renamed(x, y) == -1)
                    {
                        img.SetPixel(x, y, Color.White );
                    }
                    else
                    {
                        img.SetPixel(x, y, Color.Black);
                    }
                }
            }

            
            return img;

       }

To create a Code39 barcode using C# or any .net language first go to http://code.google.com/p/zxing/ and download the C# library and build it. Direct link for version 2 source (http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.0.zip&can=2&q=)

Add the zxing.dll file as a reference to your project.

        using System.Drawing;
        Bitmap bmp = BarCode39("Message");

        public Bitmap BarCode39(string codeMessage)
        {
            com.google.zxing.oned.Code39Writer writer = new com.google.zxing.oned.Code39Writer();
            com.google.zxing.common.ByteMatrix matrix;

            int width = 180;
            int height = 120;
            matrix = writer.encode(codeMessage, com.google.zxing.BarcodeFormat.CODE_39, 
                width, height, null);


            Bitmap img = new Bitmap(width, height);
            Color Color = Color.FromArgb(0, 0, 0);

            for (int y = 0; y < matrix.Height; ++y)
            {
                for (int x = 0; x < matrix.Width; ++x)
                {
                    Color pixelColor = img.GetPixel(x, y);

                    //Find the colour of the dot
                    if (matrix.get_Renamed(x, y) == -1)
                    {
                        img.SetPixel(x, y, Color.White);
                    }
                    else
                    {
                        img.SetPixel(x, y, Color.Black);
                    }
                }
            }


            return img;

        }

       }

I recently spent some time optimizing some 8 year old .NET code. The program would convert files, do some cleanup of data, and insert the data into a sql 2005 database. The problem was that it could take anywhere from 10 to 20 hours. It also would randomly crash and someone would need to babysit the process.

A lot of people would probably jump in at this point and start coding. I first decided to take a look at how the process was running. The program would load the data from a shared network drive as needed and then insert into a database that was also on another networked computer.

The first step was to copy all the data to the local computer before running the process. Next was to insert into a database on the local machine. This reduced the time of the process about 50%. That was good. Now we were down to 5 hours instead of 10. Next another developer noticed that the databases were set to an initial size of 1 MB and to only grow at 1MB. We changed that to an initial size of 200 MB and a growth size of I believe 10%. The process was now down to 1 hour.

The next step was to find out why it was randomly crashing. This involved reviewing the code and reviewing the process with the data technician. We discovered that most of the options in the conversion program have never been used in 8 years. So I removed over 50% of the code and streamlined the user interface. I added exception handling. Instead of just crashing the program we would now log were the errors were occurring so we could investigate further.

With this done the conversion of data was for the most part down to a few button clicks as long as there were no errors to investigate. With all the redundant code removed the process was down to 20 minutes.

A good example of useless code was every file that was converted was loaded into multiple hidden datagrids and then reset to nothing.

I still thought I could speed this up. The next step I took was to clean and split the one multi thousand line class into multiple classes. This may sound easy but it did take some work. As it would turn out the original class used class level variables for almost every variable a function used. There were only a few variables declared in the functions that needed them. So I had to sort out what functions used what variables and how they all related to each other. When I was done I had three classes that used no class level variables. All variables were declared in functions. The few places that required more then one function access to a variable had the variable passed in as a function parameter.

Cleaning the code and splitting it into three classes of course did not bring any speed improvements, but it did make it much easier to understand. If a function was passed the same parameter value it would always return the same result. This made it very easy to make the data conversion part of the program multi threaded. To be exact I used asynchronous delegates and had four conversions running at a time.

The time to run the conversion was now 2 minutes. At this point I decided it was not worth taking the optimization any further.


I have been playing around some with xwt (https://github.com/mono/xwt). I am looking forward to it reaching a release status.

Xwt is a cross platform (Window, Linux, Mac) toolkit for c#. Write your application once and it is native on all platforms. Nice.

On windows it uses wpf, linux gtk, and mac cocoa.