C#, Mono, and System-Wide Mutexes on Linux

Here’s another little “gotcha” from my experimentations in running C# apps cross-platform. The following code implements a system-wide mutex to ensure that only one instance of the program is running at any one time:

public static void Main(string[] args){
	Mutex mutex = null;
	try{
		mutex = Mutex.OpenExisting("test.martyndavis.com");
		// if we *can* open it we're an ADDITIONAL instance
		Console.WriteLine("Application is already running");
		return;
	} catch(WaitHandleCannotBeOpenedException){
		// if we get in here then the mutex doesn't exist,
		// so we are the only instance running on this machine
        mutex = new System.Threading.Mutex(true, "test.martyndavis.com");
	}
	try{
		Console.WriteLine("Doing stuff... press a key");
        Console.Read();
	} finally{
		// Although mutex implements IDisposable, it doesn't
		// call Release Mutex when being Disposed(),
		// hence usage of try/finally instead of 'using'.
		mutex.ReleaseMutex();
	}
}

Running this on Windows it behaves as one would expect. Run one instance you get “Doing stuff… press a key” output, and, if you try to run a second instance in another DOS box you get “Application is already running”.

On Linux though it’s a different story – each instance runs as if it’s the only copy on the machine.

Doing some digging turns up this note in Mono’s release docs – you can see that shared handles are now turned off by default – you can turn them on by setting the environment variable MONO_ENABLE_SHM, but for the application I’m working on I think I’ll simply avoid mutexes and use something else to ensure one instance – a lock file in the application’s data directory will be simple, and work cross-platform.

Lambda Expressions and Threads in C#

Here’s a small program… what do you think the output will be?

public static void Main(string[] args){
    Thread[] t = new Thread[10];
    for (int tNum = 0; tNum < 10; tNum++) {
        t[tNum] = new Thread(() => {
            Thread.Sleep(new Random().Next(20));
            Console.Write(tNum + " ");
        });
        t[tNum].Start();
    }
    // wait for the threads to finish
    for (int tNum = 0; tNum < 10; tNum++){
        t[tNum].Join();
    }
    Console.WriteLine("\nPress a key...");
    Console.Read();
}

Continue reading “Lambda Expressions and Threads in C#”

Using a GTK# TreeView Widget in C# – Very Simply

I’ve been experimenting with using GTK# and C# to build GUI applications which will run on Linux, Windows and Mac. Monodevelop has a graphical form designer for Gtk# front ends, but it’s not as simple as building WinForms applications under Visual Studio. An example is the TreeView widget – it’s designed from an MVC perspective, with a ListStore or TreeStore object containing the model (or data), various objects collectively representing the view (columns, cells, cell renderers etc) and the controller functions allow you to sort and filter the data. All the building needs to be done in code rather than at design time. This is a lot to wrap your head around if you simply want to display some data in a tabular format, as I did, when I wanted to display the parsed contents of a log file!

To make this easier for me in future, I’ve written a small class which hides just about everything. Read on…

Continue reading “Using a GTK# TreeView Widget in C# – Very Simply”

Kubuntu and Horrible Browser Fonts

I can’t be the only victim for this… I’ve experienced it on two different machines, on two versions of Kubuntu (11.08 and 12.04).

If I go into System Settings → Application Appearance → Fonts and select “Enabled” for “Use anti-aliasing” then click “Configure” and select “Use sub-pixel rendering”, then (after a restart probably) all the fonts in the browser (Chrome, Chromium or Firefox) look absolutely horrible. I’ve spent two days on and off trying to fix this.

The fix I’ve found is to delete ~/.fonts.conf and ~/.kderc (not sure at this stage which is the culprit), log out and back in, and, hey presto, fonts are OK again.

 

Update: example before and after screenshots can be seen here –  the top shows nice smooth fonts and the bottom, to my eye, look thin, spindly and a bit jagged.

Linux Music with Guitarix – A Fix for High CPU Usage

A quick tip for those using the excellent Guitarix guitar effects program on Linux. I regularly found both cores of my machine up around the 90%-100% when that application was running, causing XRuns in Jack and clicks in outputted audio. I just found a way around it. For some really weird reason, SSHing into my main box from a separate, really underpowered netbook (an EEE701) with the “-X” flag, then running Guitarix from that machine, results in much lower CPU usage. It’s weird because Guitarix is still running on my main machine, it’s just displayed on the remote machine. Presumably something in the display code for Guitarix is a lot heavier than it should be.