.NET Development on Linux

Eric finds out that a VM and Mono comes in handy when creating C# .NET programs. In addition to Linux, Mono lets you develop and run .NET client and server applications on Solaris, Mac OS X, Windows, and UNIX.


December 02, 2008
URL:http://www.drdobbs.com/open-source/net-development-on-linux/212201484

Eric has developed everything from data reduction software for particle bombardment experiments to software for travel agencies. He can be contacted at [email protected].


Although I started my software-development career as a UNIX programmer, for the past 20 or so years, I've worked primarily in the Windows world. However, I recently started wondering about the quality and usability of Linux distributions. I was also curious about developing C# .NET programs in Linux using Mono (www.mono-project.com), a Linux implementation of the .NET Framework. In addition to Linux, Mono (which is sponsored by Novell) lets you develop and run .NET client and server applications on Solaris, Mac OS X, Windows, and UNIX.

When I installed Ubuntu Linux (www.ubuntu.com) in a Virtual Machine (VM) on my Windows machine, I found that Ubuntu was a user-friendly and attractive OS. And while .NET development is far from perfect on the Linux platform, it is certainly possible to develop C# .NET desktop programs for Linux, and the development tools are easy enough to use that they're actually fun. In this article, I walk you through the process of installing Ubuntu in a VM, and get you started writing .NET desktop applications for Linux.

Installing VMware and Ubuntu

While it's certainly possible to install Linux on a dedicated partition on your Windows machine, it's much more convenient to install it in a VM. VMware (www.vmware.com) offers two free desktop virtualization products—the lightweight Player and more feature-rich Server. Player and Server can run Linux VMs, but only Server can create them. (You can download VMware Server 1.0.6 from www.vmware.com/download/server, but you need serial numbers from VMware. That's quick and easy.)

The following instructions cover creating a new VM and installing the OS and development tools. If you prefer to use the VM that I created, it's available online; see www.ddj.com/code/. After downloading the VM, boot it in Server, log in as user "user" with password "password," and skip to "The Sample Application" section of this article. If you're building your own VMs, download Ubuntu 8.0.4 LTS Desktop Edition (www.ubuntu.com/getubuntu/download), choose Standard Personal Computer (x86, 32-bit), and download ubuntu-8.04.1-desktop-i386.iso.

Launch Server and connect to the Local host. Click New Virtual Machine and choose Typical VM configuration. For the Guest Operating System, Select Linux and Ubuntu. In the Network Type step, specify network address translation (NAT). If you don't have Internet connectivity when you boot Ubuntu, you may need to change this option. Specify a Disk size of 10 GB, and check "Allocate all disk space now." After Server creates the virtual disk, click "Edit virtual machine settings." Select the Hardware tab and click Add. Add a Sound Adapter and choose "Use default sound adapter." Then add a USB Controller so that you can transfer files between the host and guest OS with a flash drive. The default 512 MB of RAM is sufficient, but set the number of CPUs to 2 if you have at least a dual-core machine. Select the CD-ROM device and choose "Use ISO image," and point the drive to the Ubuntu ISO file you downloaded. After you've installed Ubuntu, change this setting to "Use physical drive."

Click "Start this virtual machine" and the VM boots from the Ubuntu virtual install CD. When the Ubuntu installer loads, select your language, and pick "Install Ubuntu." Don't worry about the warning message about VMware Tools, you'll install them later. Click Forward to advance through the Install wizard. Choose the default disk-partitioning scheme. Specify a user name and password. On Step 7, click Install.

Log on when the installation completes. On the Ubuntu desktop, go to Preferences/Screen Resolution to increase the screen to a comfortable size. Enable automatic logins by going to System/Administration/Login Window. Select the Security tab and enable automatic login for the user you created. Now install VMware Tools, which will enable you to copy and paste text between the host and guest operating systems. On the VMware VM menu select Install VMware Tools. After the file browser launches, go to Accessories/Terminal. The "Terminal" is really a command shell. Enter the following commands:


$ sudo -i
# cd /tmp
/tmp# tar zxpf /cdrom/V*.gz
# cd vmware-tools-distrib
# ./vmware-install.pl


The sudo command grants the command shell the higher (root) privileges required to install software. The other commands unpack and install the software. After the install completes, set up VMware tools as a startup program. Go to System/Preferences/Sessions. In the Startup Programs tab click Add. In the Command textbox specify /usr/bin/vmware-toolbox --minimize.

Now it's time to install the development tools. Go to System/Administration and launch the Synaptic Package Manager. This utility downloads, installs, updates, and removes software packages. Click the Search toolbar button and search for "mono." Select the packages:



mono-debugger
monodevelop
monodoc
monodoc-base
monodoc-browser
monodoc-gtk2.0-manual
monodoc-http
monodoc-manual
mono-gmcs

and select Mark for Installation. When you've selected and marked them all, click the Apply toolbar button.

The Sample Application

Now it's time to try out MonoDevelop. If you created your own VM, copy the source code to the VM's filesystem. The sample app source code is available online; see "Resource Center," page 5. Download the source code and unzip it to a flash drive. Click the Safely Remove Hardware icon in your task bar and remove the flash drive from Windows. Go to Server. Select VM/Removable Devices and click on your device in the cascading menu. In the File Browser, select Edit/Select All and Edit/Copy. Click the Home toolbar button. Click Edit/Paste and the source code will be copied to your filesystem.

Select Applications/Programming/MonoDevelop. Click File/Open and navigate to /home/user/DigitsOfPi. Open the solution file DigitsOfPi.mds. Click Project/Run (or F5) to launch the application. DigitsOfPi (Figure 1) calculates p to a ludicrous precision, thanks to an algorithm written by Fabrice Bellard (bellard.org/pi/pi.c). Specify the Number of Digits and click Calculate. DigitsOfPi uses Gtk#, a managed interface into the Gnome Toolkit, instead of Windows Forms or Windows Presentation Foundation. I chose to use Gtk# for two reasons: Most Linux users prefer the Gtk look-and-feel, and Windows Forms is not available from MonoDevelop without extra setup and configuration.

[Click image to view at full size]

Figure 1: The DigitsOfPi application.

I had some problems getting the busy cursor to work properly during the lengthy calculation. I used the GdkWindow.Cursor property to set the cursors (Listing One). When I ran the program, I never saw the busy cursor because the lengthy computations prevented the GUI thread from updating the window. There are at least two ways to address this issue. If you implement lengthy computations as Idle Handlers, the busy cursor will display properly. Just put your lengthy calculations in a method and return a value of false. Make this method an Idle Handler by passing it to Glib.Idle.Add. The method will be called the next time the GUI thread is idle. Because the method returns false, it only runs once, rather than after each idle period (Listing Two). When I implemented the calculations as an Idle Handler, the busy cursor displayed correctly. But when I added a Status Bar and a Progress Bar, I noticed that progress updates were not displayed during the calculations. The solution was to call ProcessEvents after updating the Progress Bar's Fraction property. ProcessEvents executes all pending GUI events, allowing the window to redraw completely (Listing Three).


protected virtual void CalculateButtonClicked (object sender, System.EventArgs e)
{
  GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Watch);
  // lengthy computation            
  GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.LeftPtr);
}

Listing One


protected virtual void CalculateButtonClicked (object sender, System.EventArgs e)
{
  GLib.Idle.Add(OnIdleCalculatePi);
}
private bool OnIdleCalculatePi()
{
  GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Watch);
  // lengthy computation            
  GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.LeftPtr);
  // Return false to ensure that this callback
  // is not automatically called again at the next Idle moment.
  return false;
}

Listing Two


progressbar.Fraction = Math.Min((double) currentDigit / (double) totalDigits, 1.0);
GuiUtils.ProcessEvents();
 ...
public static void ProcessEvents()
{
   while (GLib.MainContext.Pending())
    {
        Gtk.Main.Iteration();
    }
}


Listing Three

Because I'm a newcomer to Gtk#, I relied heavily on online help and tooltips to figure out the Gtk# API. Note, when displaying pop-up help for overloaded methods (Figure 2), use the left and right arrow keys to scroll through the overloads.

[Click image to view at full size]

Figure 2: Pop-up help.

To create your own Gtk# application, select MonoDevelop's File/New Solution menu item. Go to the C# templates and click "Gtk# 2.0 Project." After the project is created, double-click MainWindow.cs and click the Designer button to graphically edit your program's GUI. In the toolbox window, click the Widgets and Containers triangle icons to display the controls and control containers available to Gtk# applications. Widgets can't be added directly to a window. First add a Container to the window, then add Widgets to the Container. Drag a VBox container and drop it on your window. Then drag Widgets and drop them into the VBox. The VBox container arranges the controls in a vertical grid. Containers can contain other Containers. For example, the DigitsOfPi program's GUI consists of a VBox with four rows. Row 1 contains the menus, row 2 contains an HBox that contains the number of digits spin button and the Calculate button, and so on.

Debugging and Running Mono Applications

The MonoDevelop development team is working on an integrated debugger, but for now you can debug your applications using the mdb debugger. After building a debug version of DigitsOfPi, select Applications/Terminal and enter the following commands to run the mdb debugger.

Listing Four is a transcript from one of my debugging sessions.

Use /usr/bin/mono to run your .NET applications on Linux. For example, the following command launches the debug version of DigitsOfPi:


/usr/bin/mono/home/user/DigitsOfPi/DigitsOfPi/bin/Debug/DigitsOfPi.exe


Linux distributions have achieved impressive usability levels in the last few years, especially Ubuntu. If you haven't taken a recent look at Linux, do it now. VMware Server makes it painless to experiment with Linux distributions on your Windows machine. And thanks to the Mono project and MonoDevelop, you can leverage your C# .NET skills to create full-fledged Linux applications. At least for the present, .NET development on Linux is not a developer's utopia. I found MonoDevelop's Gtk GUI designer to be a bit crash prone, and there isn't an integrated debugger yet. While I was able to build console applications with MonoDevelop and run them on my Windows machine, I wasn't able to do the same with Gtk# applications. Unfortunately, the proper version of the Gtk# library is not available for the Windows platform yet. Also, MonoDevelop is not as feature-rich or as polished as Microsoft Visual Studio. But Linux is a great platform, and it's wonderful to be able to use C# and .NET to develop Linux apps. If you haven't tried .NET development on Ubuntu, start up a VM and give it a try. Linux and Mono have come a long way in a short amount of time, and I expect them to only improve as we move forward.


$ cd /home/user/DigitsOfPi/DigitsOfPi
$ mdb bin/Debug/DigitsOfPi.exe
Mono Debugger
(mdb) start
Starting program: bin/Debug/DigitsOfPi.exe 
 ...
Thread @1 stopped at #0: 0xb78e023f in DigitsOfPi.MainClass.
Main(string[])+0x7 at /home/user/DigitsOfPi/DigitsOfPi/Main.cs:10.
  10  Application.Init ();
(mdb) b Plouffe_Bellard.cs:131
Breakpoint 4 at Plouffe_Bellard.CalculatePiDigits(int):131
(mdb) c
 ...
Thread @1 hit breakpoint 4 at #0: 0xb63fce24 in Plouffe_Bellard.
CalculatePiDigits(int)+0x84 at 
/home/user/DigitsOfPi/DigitsOfPi/Plouffe_Bellard.cs:131.
 131  for (int a = 3; a <= (2 * N); a = next_prime(a)) 
(mdb) p N 
(int) 69
(mdb) s
Thread @1 stopped at #0: 0xb63fd045 in Plouffe_Bellard.
CalculatePiDigits(int)+0x2a5 at 
/home/user/DigitsOfPi/DigitsOfPi/Plouffe_Bellard.cs:131.
 131  for (int a = 3; a <= (2 * N); a = next_prime(a)) 
Listing Four

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.