Code Contracts forthcoming in Visual Studio 2010 and .NET 4.0


I am really excited that Code Contracts are etched in to be added to the Base Class Library in .NET 4.0. This abundant excitation is genuine, so-much-so I got up at an ungodly hour this morning, planning, scheming and envisaging getting my hands on the new Library.

Code Contracts allow you to express preconditions, postconditions and object invariants in your code for runtime checking, static analysis, and documentation. You may be aware of background compilation that was added to the C# compiler in Visual Studio 2008 SP1. This is a real advance for static type languages like C# and Visual Basic, because you can start to be explicit about how your code will behave, as it is written, and errors caught even before you compile the program.

Presently, imperative programming languages are ill equipped to deal with the issue of concurrency, and the many core shift in personal computers. One of the chief obstacles in designing parallel code, is the issue of side effects. Contracts don’t offer the benefits of functional programming per se, but they do make your code more easier to predict, which is going to be a key tool in development going forward, as I don’t see all the imperative programmers in the world, and billions of lines of imperative code suddenly being converted into functional code.

The best way to demonstrate this, is through an example. Before that though, feel free to download the Code Contracts library from Microsoft Research. Presently, I am using the academic license which works with Visual Studio Professional, if you do choose to use the commercial license, you will need Visual Studio 2008 Team System. You also want to download the documentation from here, and keep an eye out for developments at the BCL Team blog.

Note: This tutorial is in both C# and Visual Basic

Common Problem

I have a simple class, that demonstrates very well (however simple it might be) what happens in real world development, pretty much on a daily basis. I am creating a class called Rational (go here if you don’t know what a rational number is) , instantiating it, and passing in a couple of values. What you you think happens when I hit F5 to run the program?

Visual Basic

Module Module1

    ‘ Simple class to represent rational numbers

    Public Class Rational

        Private numerator As Integer

        Private denominator As Integer

 

        Public Sub New(ByVal numerator As Integer, ByVal denominator As Integer)

            Me.numerator = numerator

            Me.denominator = denominator

        End Sub

 

        ‘ Method returns the closest integer by truncation

        Public Function ToInt() As Integer

            Return Me.numerator / Me.denominator

        End Function

    End Class

 

    Sub Main()

        Dim rational As New Rational(3, 0)

        Console.WriteLine(rational.ToInt())

    End Sub

 

End Module

C#

using System;

using System.Diagnostics.Contracts;

 

namespace CodeContractsDemo

{

   // Simple class to represent rational numbers

    public class Rational

    {

        int numerator;

        int denominator;

 

        public Rational(int numerator, int denominator)

        {

            this.numerator = numerator;

            this.denominator = denominator;

        }

 

        // Method returns the closest integer by truncation

        public int ToInt()

        {

            return this.numerator / this.denominator;

        }

 

    }

    class Program

    {

        static void Main(string[] args)

        {

            Rational rational = new Rational(3,0);

            Console.WriteLine(rational.ToInt());

 

        }

    }

}

 

Well you guessed it!

Visual Basic Error

OverflowException

 

C# Error

DivideByZeroException

This is such a frequent programming mistake, whereby parameters are added that end up causing problems further down the line. Who knows how long it has been since the rational class was first created, and the operation of passing in the zero denominator?

Note: When you installed Code Contracts from Devlabs, the necessary .dll was added to your machine. In C# right click the references node in Solution Explorer and go to add a reference (In Visual Basic double click MyProject in Solution Explorer and select the References tab on the left and select add) to Microsoft.Contracts Library. This will be included in MSCORLIB in .NET 4.0.

ContractsLibrary

You should now find that you can include the System.Diagnostics.Contracts namespace to the top of your class

Visual Basic

Imports System.Diagnostics.Contracts

C#

using System.Diagnostics.Contracts;

Code Contracts

Code Contracts are designed to preclude such eventualities from ever occurring in the first place, and to prevent them really early on, without the need to run the program first. This in some ways illustrates the limits of background compilation, which itself is not a limitation, but the point of inflexion whereby a developer starts to create an algorithm. The background compiler cannot deduce what a developer wants to do beforehand, if it could, we’d all be out of a job.

I need a way to disallow the client from ever passing in a zero denominator. Typically, today, I would write some parameter validation code in the constructor, that throws an exception.

Add the following code to the constructor of the class above (I have decided I want just positive numbers now)

Visual Basic

     Public Sub New(ByVal numerator As Integer, ByVal denominator As Integer)

            Contract.Requires(0 < denominator)

            Me.numerator = numerator

            Me.denominator = denominator

        End Sub

C#

      public Rational(int numerator, int denominator)

        {

            Contract.Requires(0 < denominator);

            this.numerator = numerator;

            this.denominator = denominator;

        }

When you installed the contracts library, a new Code Contracts tab was added to your project. In C# double click the Properties node in Solution Explorer in Visual Basic, double click MyProject in Solution Explorer

VisualStudio

Place a check in the “Perform Runtime Contract Checking” checkbox, return to the project and hit F5 to run the project.

You should find you have the following (Note: the failure is a Precondition)

PreCondition

This Debug.Assert is called upon instantiation of the class, and does not occur further down the line when ToInt() is called.

Return to the Code Contracts tab, and this time check “Perform Static Contract Checking” and build

StaticAsWell

You should now find that you have warnings in your error list

Visual Basic

VBError

C#

CSharpError

You don’t need to execute your code, the static checker runs a build time which is pretty smart. It is a best practice to always enable runtime checking as well, as the static checker does not always catch every single error, but the real power in this, is that every precondition in the solution is checked.

Change the zero parameter in the constructor thus, and build

Visual Basic

    Sub Main()

        Dim rational As New Rational(3, 4)

        Console.WriteLine(rational.ToInt())

    End Sub

C#

     static void Main(string[] args)

        {

            Rational rational = new Rational(3, 4);

            Console.WriteLine(rational.ToInt());         

        }

It is still possible for a zero denominator to enter the ToInt() method, even though it appears impossible due to the constructor code. One might have another method in this class or have sub types of this class, and there is nothing to prevent this Rational class from getting a zero denominator even though the class was not created with a zero in the first place. The way one protects the class from this occurring is through an object invariant method.

In the code editor type “cim” and then tab tab – just like adding an event handler (no code snippets yet in the VB editor)

ContractInvariantMethod 

Visual Basic

     <ContractInvariantMethod()> _

        Protected Sub ObjectInvariant()

            Contract.Invariant(False)

        End Sub

C#

      [ContractInvariantMethod]

        protected void ObjectInvariant()

        {

            Contract.Invariant(false);

        }

 

You can then set the parameter in the method

Visual Basic

        <ContractInvariantMethod()> _

        Protected Sub ObjectInvariant()

            Contract.Invariant(Me.denominator > 0)

        End Sub

C#

   [ContractInvariantMethod]

        protected void ObjectInvariant()

        {

            Contract.Invariant(this.denominator > 0);

        }

It is now impossible for the denominator in the class to ever be zero. One also want to be able to inform clients of this Rational class what the ToInt() returns, this is the postcondition.

Visual Basic

        ‘ Method returns the closest integer by truncation

        Public Function ToInt() As Integer

            Contract.Ensures(Contract.Result(Of Integer)() >= 0)

            Return Me.numerator / Me.denominator

        End Function

C#

        // Method returns the closest integer by truncation

        public int ToInt()

        {

            Contract.Ensures(Contract.Result<int>() >= 0);

            return this.numerator / this.denominator;

        }

The thing to notice here is that the Contract.Ensures method is declared before the integer is returned, but the compiler (magic) rearranges the code so this code is called after the integer has been returned back. I will now show the completed projects showing preconditions, object invariants and postconditions.

Visual Basic

Imports System.Diagnostics.Contracts

 

Module Module1

    ‘ Simple class to represent rational numbers

    Public Class Rational

        Private numerator As Integer

        Private denominator As Integer

 

        Public Sub New(ByVal numerator As Integer, ByVal denominator As Integer)

            Contract.Requires(0 < denominator)

            Contract.Requires(0 <= numerator)

            Me.numerator = numerator

            Me.denominator = denominator

        End Sub

 

        <ContractInvariantMethod()> _

        Protected Sub ObjectInvariant()

            Contract.Invariant(Me.denominator > 0)

            Contract.Invariant(Me.numerator >= 0)

        End Sub

 

 

        ‘ Method returns the closest integer by truncation

        Public Function ToInt() As Integer

            Contract.Ensures(Contract.Result(Of Integer)() >= 0)

            Return Me.numerator / Me.denominator

        End Function

 

    End Class

 

    Sub Main()

        Dim rational As New Rational(12, 3)

        Console.WriteLine(rational.ToInt())

    End Sub

 

End Module

C#

using System;

using System.Diagnostics.Contracts;

 

namespace CodeContractsDemo

{

   // Simple class to represent rational numbers

    public class Rational

    {

        int numerator;

        int denominator;

 

        public Rational(int numerator, int denominator)

        {

            Contract.Requires(0 < denominator);

            Contract.Requires(0 <= numerator);

 

            this.numerator = numerator;

            this.denominator = denominator;

        }

 

        [ContractInvariantMethod]

        protected void ObjectInvariant()

        {

            Contract.Invariant(this.denominator > 0);

            Contract.Invariant(this.numerator >= 0);

        }

 

 

        // Method returns the closest integer by truncation

        public int ToInt()

        {

            Contract.Ensures(Contract.Result<int>() >= 0);

            return this.numerator / this.denominator;

        }

 

    }

    class Program

    {

        static void Main(string[] args)

        {

            Rational rational = new Rational(12,3);

            Console.WriteLine(rational.ToInt());         

        }

    }

}

 

I am sure one can be certain that you be seeing a lot more of this type of .NET code in the not too distant future.

Windows Vista (and Windows 7) Bridge for managed developers


To quote the Windows SDK Team

“Many of you don’t know about the Vista Bridge sample library, what can it do or even where you can find it.

Let’s start with the basics; i.e. definitions. Vista Bridge is a sample library that contains managed wrappers for a number of Windows Vista APIs that are not exposed in the .NET Framework. The wrappers include common Open and Save file dialogs and new Vista advanced task dialogs.”

This SDK is very late ( by at least a couple of years) and contains a lot of features managed developers have moaned and moaned about being missing.

Finally, now, in version 1.4, we are nearing something that developers can use in their applications. The new Windows 7 taskbar – in fact all of the Windows 7 API’s – will be included in future versions of the Vista Bridge. If you are looking to develop your managed application targeting the new Windows 7 API’s, then the code is available here.

The great thing is that you can use both Windows Forms and Windows Presentation Foundation, though the samples are mostly WPF.

Libraries

Note: There is a CHM with information on how to use the controls in Windows Forms, and hopefully a fully documented Visual Basic CHM as well. Here are a few screenshots of the controls

Aero Wizard

AeroWizard

Breadcrumb Control

BreadCrumb

Vista and Windows 7 Interop

VistaInteop

From the Interop samples above you have

ComplexTaskDialog

Customised Open File Dialog

OpenFileDialogCustomised

So, all-in-all there are some very useful controls that I hope will be fully released in the Windows 7/Visual Studio 2010 time frame, and will be incorporated into Visual Studio 2010.

If you develop smart client applications that will be targeting Windows 7, then I really do urge you get to know and use the Windows 7 Taskbar API’s. The Windows team have dramatically changed the way that the taskbar is used, with reduced repetition, jump lists and so forth, with the common file dialog being an axis that the Windows 7 Taskbar is built upon.

SQL Compact and The Entity Framework


Linq to SQL Vs Entity Framework

Linq to SQL is not supported using SQL Compact, although it is possible to use Linq to SQL using SQLMetal.exe, I have a post here showing how this can be done. The Entity Framework however, is fully supported in SQL Compact CE.

Personally speaking, if I was to create a large application that is data access heavy (especially if it is multi-user), then I would still opt for ADO.NET datasets and stored procedures. There are quite a few applications that don’t require the the fuss of creating and maintaining stored procedures, and in those cases, I definitely prefer using an Object Relational Mapping tool (ORM).

Linq to SQL Development Terminated

Microsoft announced that they would stop any further development of Linq to SQL. This (as you might guess) was not received at all well in the developer community – just read the comments in the announcement – because this little ORM tool gained phenomenal popularity very quickly, mostly because it was very lightweight though still very powerful.

The Entity Framework has not been received as favourably, with many dissatisfied by the complexity and ultimately the cogency of the object model. For me, however, the Entity Framework is just an ORM tool like Linq to SQL, and contains pretty much all the functionality, so I see no point in not using it, especially since resources are continuing to be poured into it, and the same Linq queries you ran in Linq to SQL are the same you run as Linq to Entities.

Master Details Demo

For this demo (note this demo is in both C# and Visual Basic), I am going to create a simple master/details form that collects data from the Northwind database, make some changes and save those changes. This is very typical for any type of application that would use a compact database.

Create a new .NET 3.5 windows forms project and call it NorthwindTraders.

Add a new item to the project

AddNew

Choose the ADO.NET Entity Data Model and name it NorthwindModel and clock add

NorthwindModel

You should now have the Entity Data Model Wizard, choose “Generate From Database” and click “Next”

Wizard

Here you can choose the database type which can be SQL 2008, Express or SQL Compact. Since this demo is about SQL compact, change the datasource by clicking the change button

DataBaseType

And choose SQL Compact and click OK

SQLCompact

When you click “Browse to locate” a database, you should automatically open the Northwind.sdf located at C:Program FilesMicrosoft SQL Server Compact Editionv3.5Samples

BrowseForDB

I never run as administrator when developing in general (on Vista/Wiindows 7) so you will need to move this sample database to a folder where you can access it without running as administrator. I have copied the database to a folder on my C drive, select the database there and click OK.

You should now have the connection string, and set the connection string to NorthwindEntities and click “Next”.

ConnectionString

Choose the “Customers” and “Orders” tables and click “Finish”

Model

IF you double click the NorthwindModel.edmx in Solution Explorer you should have the ORM in the designer.

ModelInDesigner

Note: The designer has a slight bug in that if the scroll bar is right at the top (see red arrow above), then you cannot see the mappings, to correct this, just move the scrollbar down a little

From the Data menu, add a data source

AddDataSource

Select “Object” and select “”Next

Customers

Click “Finish” and you should now have the datasource showing

Add a SplitContainer to the form and set the orientation to “horizontal”. Drag a DataGrid from the Customers Node into the first split panel, and do the same with the related orders

MasterDetails

You should now have the two datagrids, the two binding sources and a binding navigator. Set the save button in the navigator to “Enabled”. Double click the form to create a Form_Load event and also double click the save button to create an event handler

Enabled

In the code behind of the form (note Visual Basic code is beneath)

C#

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace NorthwindTraders

{

    public partial class Form1 : Form

    {

        NorthwindEntities context;

 

        public Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        /// When the form loads we instantiate the NorthwindEntities context and perform

        /// a simple Linq query that returns all the customers in London

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_Load(object sender, EventArgs e)

        {

            context = new NorthwindEntities();

 

            var customers = from c in context.Customers.Include("Orders")

                            where c.City == "London"

                            select c;

 

            this.customersBindingSource.DataSource = customers;

        }

 

        /// <summary>

        /// Save the changes made in both DataGridviews

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void customersBindingNavigatorSaveItem_Click(object sender, EventArgs e)

        {

            this.context.SaveChanges();

 

        }

 

 

    }

}

Visual Basic

Public Class Form1

 

    Private context As NorthwindEntities

 

    ”’ <summary>

    ”’ When the form loads we instantiate the NorthwindEntities context and perform

    ”’ a simple Linq query that returns all the customers in London

    ”’ </summary>

    ”’ <param name="sender"></param>

    ”’ <param name="e"></param>

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        context = New NorthwindEntities()

 

        Dim customers = From c In context.Customers.Include("Orders") _

            Where c.City = "London" _

            Select c

 

        Me.CustomersBindingSource.DataSource = customers

    End Sub

 

    ”’ <summary>

    ”’ Save the changes made in both DataGridviews

    ”’ </summary>

    ”’ <param name="sender"></param>

    ”’ <param name="e"></param>

    Private Sub CustomersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomersBindingNavigatorSaveItem.Click

        Me.context.SaveChanges()

    End Sub

End Class

As you can see, it really is quite easy to use the EntityFramework, and pretty much the same functionality in Linq to SQL is available here.

The importance of the Background Worker component


Serious Problem

I recently reviewed a very large enterprise application for one of the biggest retail companies in the country, and was aghast at the lack of multi-threading in the application. I resolved to ensure I post about it, though this subject has been covered numerous times over the years, and there are a few examples interspersed over the internet. New developers or maybe intermediate developers not working in the data access layer of a project may gloss over why this component is so important. You also don’t want to revisit an application in a few years time and have to make multi-threading modifications.

I came to realise the true importance of this component, because as I spoke with staff, and they talked me through the program working, at key stages during the program I would be told that after clicking a button that makes a database call  “the computer is thinking now, so wait for a while” or “when you click this, you cannot do anything”, or “don’t click that option because it will freeze the screen, and you won’t be able to use the computer for half an hour”.

The list of these warnings goes on and on, about using their system. The administration staff/information workers have tasks they have to run several times every day, and typically, they cannot use their system for half and hour or even more at times, because the queries they are running are executing on the UI thread. This quite frankly is an atrocious situation. Pretty much every part of their application that makes database calls, runs on the UI thread.

What ‘beggars belief’ is that this system is used by one of the largest retail companies in the UK, and that there are doubtlessly more retailers that are in the same boat. These retail organisations realised the power of computing many years ago, and their systems were developed at a time when patterns and practices had not been invented, and some very bad coding practices were cultivated. I estimate that this specific company that has triggered this post, is losing at least £100 000 annually in wages and lost productivity, because all their staff spend a third of their day twiddling their thumbs, while their computer screens are frozen because the UI thread is locked up (and this is a conservative estimate).

Demonstration

Seriously, if you develop any application that makes calls to a database you must learn to love and use this component on every form that makes a database call!

Typically you will create an interface that has a loadDataBackgroundWorker (when the form loads) and a saveDataBackgroundWorker (when the form is closed). There really is no excuse not to use this component, which incidentally, is framework agnostic, and is used in the new Silverlight 2 runtime. I know of some people that have run into issues with the dispatcher component in WPF, and they are using this in their WPF applications. In general, writing multithreaded applications is very difficult, the background worker component makes writing multithreaded applications so much easier.

To demonstrate how this works, create a new windows forms application in C# or Visual Basic (you can use the express editions) and drag a progress bar and two buttons onto the form. Name the appropriate button “startButton” and “MessageButton”.

TheForm

In the code behind, handle the events thus (Visual Basic example is beneath)

C#

using System;

using System.ComponentModel;

using System.Windows.Forms;

 

namespace BackgroundWorkerDemo

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            this.progressBar1.Minimum = 0;

            this.progressBar1.Maximum = 9;

            this.progressBar1.Value = 0;

        }

 

        private void messageButton_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Hello World!");

        }

 

        private void startButton_Click(object sender, EventArgs e)

        {

            for (int i = 1; i < 10; i++)

            {

                this.progressBar1.Value = i;

                System.Threading.Thread.Sleep(1000);

            }

        }

    }

}

Visual Basic

Imports System.ComponentModel

 

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.progressBar1.Minimum = 0

        Me.progressBar1.Maximum = 9

        Me.progressBar1.Value = 0

    End Sub

 

    Private Sub messageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles messageButton.Click

        MessageBox.Show("Hello World!")

    End Sub

 

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click

        For i As Integer = 1 To 9

            Me.progressBar1.Value = i

            System.Threading.Thread.Sleep(1000)

        Next

    End Sub

End Class

If you run the program, and click on the “show message” button, this shows the message “Hello World!”. If you then click on the start button and try to click the “show message” button you will find that you cannot trigger the message, until the progress bar has completed. Incidentally, System.Threading.Thread.Sleep(1000) just causes the thread the progress bar is running on to pause for a second (1000 milliseconds). If this were not included, the for loop would execute too fast, and you would not see the screen locking up.

This scenario is no different to the problem I have outlined by this major enterprise application. When users of the application click “load orders” or whatever option fetched data from the database, their application is locking the UI thread, and this leads to a very poor user experience, and statements like “the computer is thinking about things”.

Solution

Drag a background worker component onto your form from the toolbox, and set the reports progress property to true

ReportsProgress

Click the events button and handle all 3 events

BackGroundEvents

In the event handlers move the previous code so it looks like this

C#

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace BackgroundWorkerDemo

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            this.progressBar1.Minimum = 0;

            this.progressBar1.Maximum = 9;

            this.progressBar1.Value = 0;

        }

 

        private void messageButton_Click(object sender, EventArgs e)

        {

            MessageBox.Show("Hello World!");

        }

 

        private void startButton_Click(object sender, EventArgs e)

        {

            this.backgroundWorker1.RunWorkerAsync();

        }

 

 

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

        {

            for (int i = 1; i < 10; i++)

            {

                this.backgroundWorker1.ReportProgress(i);

                System.Threading.Thread.Sleep(1000);

            }

        }

 

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            this.progressBar1.Value = (int)e.ProgressPercentage;

        }

 

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            this.progressBar1.Value = 0;

        }

    }

}

Visual Basic

Imports System.ComponentModel

 

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.progressBar1.Minimum = 0

        Me.progressBar1.Maximum = 9

        Me.progressBar1.Value = 0

    End Sub

 

    Private Sub messageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles messageButton.Click

        MessageBox.Show("Hello World!")

    End Sub

 

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

    Handles startButton.Click

        Me.BackgroundWorker1.RunWorkerAsync()

    End Sub

 

    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) _

    Handles BackgroundWorker1.DoWork

        For i As Integer = 1 To 9

            Me.BackgroundWorker1.ReportProgress(i)

            System.Threading.Thread.Sleep(1000)

        Next

    End Sub

 

    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) _

    Handles BackgroundWorker1.ProgressChanged

        Me.progressBar1.Value = CInt(e.ProgressPercentage)

    End Sub

 

    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) _

    Handles BackgroundWorker1.RunWorkerCompleted

        progressBar1.Value = 0

    End Sub

End Class

When you run the application and click the message button after you click start, the long task (i.e. the work done connecting to your database) is executed on another thread asynchronously. this results in a responsive application that does not lock. The users at the aforementioned enterprise can then click into other options in their application and do other work, while the program processes.

Quick Tip : Ensure that you do not add any code that accesses the UI thread in the DoWork event. You may have a label that says connecting…. in your ToolStripLabel for instance. Make sure this code is where the backgroundWorker.RunWorker.Async() is called, and nowhere else. You can add a message so say “completed” or something similar in the RunWorkerCompleted event as that is on the UI thread.

I really cannot reiterate sufficiently clearly just how important it is to wrap every method in your application that makes a database call in a background worker. Not only  does this improve the efficiency and productivity of employees (users), but they come away with a very positive view of the application, which is not what the users at this company have. When training new staff they end up being on tenterhooks just in case they click on the wrong button, and cause the program to freeze. At busy times, or near the end of the day where work needs to be completed, the last thing you need, is an application that hangs for half an hour or even longer before you can use it again.

Please, please, please, use this component as much as you can in all your data access applications.

Windows 7 TreeView and ListView


The default Tree and List view controls in windows forms look rather dated. In an earlier post, I demonstrated just how easy it was to change the theme of the TreeView control and ListView controls so they look like Vista’s explorer.

The same code can be used to attain the Windows 7 Tree and List view controls look and feel.

TreeAndList

Here I have a TreeView with an ImageList, and a ListView with an ImageList that has some large images, and am populating it when the form loads. If you would like to go further with the other controls (buttons etc.), there is a very good article on codeproject.com worth checking out.

Visual Basic (c# example beneath)

Imports System.Runtime.InteropServices

Public Class Form1

    Private random As Random

 

    Public Sub New()

        Me.Font = System.Drawing.SystemFonts.IconTitleFont

 

        InitializeComponent()

 

        SetWindowTheme(treeView1.Handle, "explorer", Nothing)

        SetWindowTheme(listView1.Handle, "explorer", Nothing)

 

        Me.treeView1.HotTracking = True

        Me.treeView1.FullRowSelect = True

    End Sub

    ”’ <summary>

    ”’ Native interop method, be sure to include the  System.Runtime.InteropServices

    ”’ name space

    ”’ </summary>

    ”’ <param name="hWnd"></param>

    ”’ <param name="appName"></param>

    ”’ <param name="partList"></param>

    ”’ <returns></returns>

    <DllImport("uxtheme.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)> _

    Private Shared Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal appName As String, ByVal partList As String) As Integer

    End Function

 

 

    ”’ <summary>

    ”’ Using an image collection in an ImageList component, fill the

    ”’ ListView control with 50 random images when the form loads

    ”’ </summary>

    ”’ <param name="sender"></param>

    ”’ <param name="e"></param>

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        random = New Random()

 

        Me.treeView1.ExpandAll()

        Me.listView1.LargeImageList = Me.imageList2

        Me.listView1.BeginUpdate()

 

        For i As Integer = 0 To 49

            Dim item As New ListViewItem()

 

            item.ImageIndex = random.[Next](0, imageList2.Images.Count)

 

            Dim name As String = DirectCast(item.Name, String)

 

            item.Text = "Item " & name

 

            Me.listView1.Items.Add(item)

        Next

 

        Me.listView1.EndUpdate()

    End Sub

 

 

End Class

C#

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace WindowsSevenTreeViewAndListView

{

    public partial class Form1 : Form

    {

        Random random;

 

        public Form1()

        {

            this.Font = System.Drawing.SystemFonts.IconTitleFont;

 

            InitializeComponent();

 

            SetWindowTheme(treeView1.Handle, "explorer", null);

            SetWindowTheme(listView1.Handle, "explorer", null);  

 

            this.treeView1.HotTracking = true;

            this.treeView1.FullRowSelect = true;

        }

        /// <summary>

        /// Native interop method, be sure to include the  System.Runtime.InteropServices

        /// name space

        /// </summary>

        /// <param name="hWnd"></param>

        /// <param name="appName"></param>

        /// <param name="partList"></param>

        /// <returns></returns>

        [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]

        private static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList);

 

        /// <summary>

        /// Using an image collection in an ImageList component, fill the

        /// ListView control with 50 random images when the form loads

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_Load(object sender, EventArgs e)

        {

            random = new Random();

 

            this.treeView1.ExpandAll();

            this.listView1.LargeImageList = this.imageList2;          

            this.listView1.BeginUpdate();

 

            for (int i = 0; i < 50; i++)

            {

                ListViewItem item = new ListViewItem();

 

                item.ImageIndex = random.Next(0, imageList2.Images.Count);

 

                string name = (string)item.Name;

 

                item.Text = "Item " + name;

 

                this.listView1.Items.Add(item);

            }

 

            this.listView1.EndUpdate();

        }

    }

}

Unable to find manifest signing certificate in the certificate store


I have recently upgraded operating systems from Windows Vista to Windows 7 beta 1. When attempting to run a program that complied on Vista, I get the error message; Unable to find manifest signing certificate in the certificate store. [name of the project]

A brief Google search shows this is a common error, but no-one seems to have the error I do, as the suggestion is to edit the .cs.project file and remove the manifest signing section which I presently do not have.

The cause of this error is click once. Whenever you use click once, it creates a temporary strong key name like this one

tempkey1

 

 

 

 

 

 

 

 

 

 

It is this key that is missing, hence the compilation error. Luckily to create another temporary key is easy. Double click into the properties node in  the Visual Studio Solution Explorer, select the "signing" tab and click on Create Test Certificate…certificate1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Don’t bother entering a password, click on OK, and you should now find a new temporary key has been created and added, so your project will now compile.