As you all might now, Doppler and DopplerMobile have been developed in C#. The initial versions where based upon .NET 1.1, the later releases (Doppler 3.0 is due out as a beta in January) are based upon .NET 2.0.

Given my background in Java coding it would have made a lot of sense at first view to start developing tools like Doppler and DopplerMobile in Java. However, there is one thing that made me use .NET. It’s the ease of developing a GUI. Visual Studio .NET 2003/2005 (or if you don’t want to use these products, go for the open source SharpDevelop) allow me to build up a prototype in minutes. From there on is basically finetuning (okay, I’m exaggerating a bit, but you get my point).

But really, the ease of which I can build up a UI which looks familiar to the majority of users (which means to me: it looks like a windows application) was the thing that converted me.

C# in itself is a great language. It has all things you’re looking for in a OO language, and if you’re familiar with java, you will be up and running in minutes. Really. A nice overview of the differences can be found at http://www.25hoursaday.com/CsharpVsJava.html.

If you’re new to threading (Doppler uses a lot of threading) then that concept can be quite complex actually. One thing to know is that a .NET application is basically a ‘thread’. And the main form, the main window so to say, runs in that main thread. Now the problem comes when you want to update an screen element from a thread doing some other things, say, downloading a file and you want to update a progress bar on the main form for instance. In .NET you cannot update controls (design elements, like buttons, text fields etc.) from another thread. This can be solved by using invokes on the controls and using delegates etc, but that goes beyond the scope of this post.

Now in .NET 2.0 they introduced the ‘BackgroundWorker’. It’s VERY simple to implement.

Basically you drop it on your form (if you’ve never seen Visual Studio at work this might sound a bit abstract) and it will create an object called backgroundWorker1 in your form class. The BackgroundWorker has a few properties like if it should report progress and if it should support cancellation. It also has a few methods (actually they’re event handlers, but think of them as methods you invoke on the object).

Just double click on the newly created backgroundWorker1 (although it is a non visual class it still shows as a ‘clickable’ item on your form for easy reference) and Visual Studio will create an empty method called BackgroundWorker_doWork(). In there you write your code that should run in a separate thread.

Then when you want to run the code in that thread execute the worker with backgroundWorker1.RunWorkerAsync(). That’s all there is to it!

An short code example:

namespace demo
{
public partial class Form1 : Form
{
public Form1() // this is the constructor
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// put here your code that you want to run in a separate thread
}
}
}

Don’t you love the simplicity? I for sure do! A nice thing is that you can actually update controls/buttons/texts etc from the DoWork method as the backgroundworker will take care of the invoking etc.
In the example above a few things are noteworthy, in C# a java ‘package’ is called a ‘namespace’. ‘extends’ is written down with a ‘:’ and the most interesting thing is new in the latest version of C#, a ‘partial’ class.

A partial class is a way to allow you as a developer to split up the code of your class into several physical files. Then at compile time all those ‘partial classes’ will be combined into one class. The Visual Studio forms designer uses this for instance to put all the code which makes up the form (which button ends up where at the form, which list box shows where, what is the background of the form etc. etc.) in a separate C# file. You -can- modify the code, but you don’t have to. Using a partial class you basically only end up with your business logic on screen. The rest is there but you don’t have to see it if you don’t want to. I really like that concept. Of course you can also use this to split up the coding of class over several developers if required for instance.

These small things really make me like C# and .NET. It allows me to create applications fast, and if needed, I can still go in and modify all the nitty-gritty-details.