February 11, 2008#

.NET Programming: Single Instance Application

Few days ago, I did some research on how to make sure that only one instance of my .NET application will run at a time. I found there’s three ways to do so, of which two are quite common and also very stable and professional.

Solution Number 1: Using a Mutex

Just have a look at the code snippet below. This is a simple C# program with just a few lines added.

   1:  static class Program
   2:      {
   3:          static bool bFirstInstance = false;
   4:          static Mutex mtxSingleInstance = new Mutex(true, "My Mutex Name and ID String", out bFirstInstance);
   6:          /// <summary>
   7:          /// The main entry point for the application.
   8:          /// </summary>
   9:          [STAThread]
  10:          static void Main()
  11:          {
  12:              try
  13:              {
  14:                  if (!bFirstInstance)
  15:                      return;
  17:                  Application.EnableVisualStyles();
  18:                  Application.SetCompatibleTextRenderingDefault(false);
  20:                  Application.Run(new Form1());
  21:              }
  22:              finally
  23:              {
  24:                  mtxSingleInstance.Close();
  25:              }
  26:          }
  27:      }

The important lines are lines 3 and 4 and 14 and 15. First we create a mutex with any name (string) we like and try to access it. The result (whether gaining access to the mutex failed or succeeded) will be written to a boolean variable. If this is the first instance of the application, we sucessully will gain access to the mutex and bFirstInstance will be true. If it’s a second, third, … instance, the first instance of the application will already have locked the mutex and we won’t get access to it, thus bFirstInstace will be false.

We just check for the value of bFirstInstance and if it’s false, the current instance will exit immediately.

Important Note: Make sure the mutex gets closed when the first instance of your application exits (even in case it crashes and doesn’t exit normally). You can use a try / finally statement as shown above for this.

Another important thing: You should declare the mutex as a static class variable to make sure it won’t get cleaned up by garbage collection somewhere down the road. Instead of using a global static variable, you could also make use of GC.KeepAlive(mtxSingleInstance);.

For further information see the two articles here and here.

Solution Number 2: .NET Remoting

This is basically about setting up some kind of inter-process communication so one process can just shout out ‘Hey, is there someone of my kind already running?’ on some channel and other instances of this process will respond with ‘Yep, that would be me’ or whatever.

The great advantage over the method before is, that with this solution, you cannot just check for other instances already running but you can also exchange information between the two instances (like passing the command line parameters of the second instance to the first instance and let the second instance exit after this while the first instance will do the job).

Now despite this solution sounding a little bit complicated, it actually isn’t. In Visual Basic this is just activating a checkbox and in C# it’s about ten lines of code. For the Visual Basic solution, go here. For C# do the following:

  1. Add a reference to Microsoft.VisualBasic.dll to your project
  2. Add the using Microsoft.VisualBasic.ApplicationServices; statement to your main application class.
  3. Add a new class to your project, lookig like this:
    using Microsoft.VisualBasic.ApplicationServices;
    public class SingleInstanceApplication : WindowsFormsApplicationBase
        private SingleInstanceApplication()
            base.IsSingleInstance = true; 
        public static void Run(Form f, 
            StartupNextInstanceEventHandler startupHandler)
            SingleInstanceApplication app = new SingleInstanceApplication();
            app.MainForm = f;
            app.StartupNextInstance += startupHandler;
  4. Now change your main application class from this

       1:  static class Program
       2:  {
       3:      [STAThread]
       4:      static void Main()
       5:      {
       6:          Application.EnableVisualStyles();
       7:          Application.Run(new Form1());
       8:      }
       9:  }

    to this

       1:  static class Program
       2:  {
       3:      [STAThread]
       4:      static void Main()
       5:      { 
       6:          Application.EnableVisualStyles();
       7:          SingleInstanceApplication.Run(new Form1(), 
       8:              StartupNextInstanceHandler);
       9:      }
      11:      static void StartupNextInstanceHandler(
      12:          object sender, StartupNextInstanceEventArgs e)
      13:      {
      14:          // Here you can just make the current (new) instance exit or do whatever you like with the command line arguments
      15:      }
      16:  }

That’s it!

I found this idea and the code above here.

Solution Number 3: Checking the Process List

I’m not gonna go into detail here, as this shouldn’t be the preferred solution as I think. It’s just for the record. To check whether there’s already an instance of your application running, you can of course just read the systems list of processes currently running and see if your application’s in it or not!

Happy coding!

February 2, 2008#

FSXGET 0.1 Beta 1 Released

logo Today I’ve released version 0.1 Beta 1 of FSXGET. It’s the direct successor to version RC4. The change in naming is to make the version numbers more readable and manageable.

Remote Connection – Run FSX and FSXGET on different computers

The new version includes a new configuration dialog that facilitates setting up FSXGET to connect to Flight Simulator X running on a remote computer, i.e. a configuration where Google Earth AND FSXGET run on one computer but Flight Simulator X runs on another. I’ve also released a new tool called SimConnect Config Tool which helps changing Flight Simulator X configuration to allow remote access (e.g. from FSXGET). A howto article about setting up FSX and FSXGET for remote connections can be found here.

January 30, 2008#


logo Just released a new update for FSXGET correcting bugs regarding x64 and non-English systems. Download and more information here.

January 29, 2008#


Anyone remember the wikiscanner, a tool cross-referencing IP ranges known to belong to a certain company with IP addresses of Wikipedia article edits, thus exposing which companies have edited their own Wikipedia entries (and what they’ve edited)?

I did really like this project unlike most politicians, governments and companies exposed by the tool I guess. Anyway, it’s time for Vol. 2! Books that make you dumb!

It’s from the same author again, this time cross-referencing colleges’ students’ average SAT scores with colleges’ most read books lists on Facebook and there you go. You’ll be able to tell what clever guys read and what dumb people read ;)


Of course, one can argue about any causality in this and apparently not too few people were quite willing to do so as the author states on his website but there’s actually not a single word on his website blaming any of those books for dumbness or cleverness of its readers. Just consider it a fun thing and enjoy reading through the list!

January 29, 2008#


Most people reading this probably won’t find it very funny. I do though!

I can’t say whether I like it more for its being a joke about Hamilton (I’m a Ferrari fan) or the Spanish stubbornness of pronouncing foreign words the way they fit best into their own language regardless of how they should be pronounced or how absurd they might sound, which puts another joke in the banner below. Anyway, I just couldn’t help smiling when I first saw it.


January 26, 2008#

Thanks for your help starryalley

planetarium-thumbI’ve just found out that one of my projects, a web-based planetarium found a mention on a Taiwanese blog. It’s the blog of a programmer from Taiwan whose application I used in my project and who helped me out with some problems I had when trying to adapt his application to my needs.

You can see his blog entry about my project here. But anyway, I’d like to use that chance to thank him again for his help. He did really save me a lot of time just as did his application.

So, thanks again starryalley for your help and keep up such a good work with mobile starchart.

January 22, 2008#

Star Trek XI Trailer on the Web

Somehow this trailer has made its way to the web today. The movie itself is due December this year if I remember right. So till then, enjoy the trailer:

January 22, 2008#

Windows Vista – A Fairy Tale Gone Bad

This is going to be a short post about my first year of experience with Windows Vista. Now I’m not gonna tell you that you should use Linux or Windows XP instead of Vista which is really really bad, as you can read on so many blogs and forums but I want to give an honest opinion on Vista’s look and feel and point out some things which are pretty odd and which I really can’t understand.

So first of all: I actually like Windows Vista. Built-in search, nice GUI and especially that extremely improved way of navigating folders in Windows Explorer are features I benefit from. Control over thumbnail sizes is another nice feature of Windows Explorer. The sidebar is ok, too, though one can clearly see that Microsoft didn’t spend too much time developing that one.

Now to the things I don’t like and often not even understand:

1) During the first few month of usage, I couldn’t plug-in any USB flash drive with more than 500 MB, as Vista would crash with a BSOD (blue screen of death) in result. Man! How did Microsoft achieve such a bug?!? I’m not talking about that very special time dilating warp plasma machine I’m trying to connect via USB… it’s a USB FLASH DRIVE. I’ve been really annoyed by that matter but couldn’t do much about it. Finally after few months living with it, some Microsoft fixed the bug and since then, I’m able to use my 2 GB flash drive again. :-)

2) Windows BackUp tool! It’s gotten a clearly improved GUI, I must admit that. But for the cost of features which are now missing. It’s no longer saving to some *.bkf file but to *.zip files each of which cannot be larger than 100 MB. So you end up with dozens of *.zip files in the end. But the most annoying thing is that it can no longer backup EFS encrypted files. They are just left out. There’s no longer any way to backup them in Windows Vista, besides doing it manually. But why?!? All you gotta do now to keep files from getting backed up is to set that ‘encryt file’ checkbox in Windows Explorer. Aren’t encrypted files important?!? Now I have that nice and easier to use BackUp tool (NT BackUp was clearly outdate) in Windows Vista, but it’s not worth a cent as I’m not even using it. I need ALL my file to be backed up not just some of them, so I have to use some other backup solution.

3) Same with Windows Disk Defragmentation. Now it’s got an easier to operate GUI but somehow Microsoft forgot to include some kind of program feature. The disk fragmentation visualization overview is gone, too. I can understand the latter one, as for sure, it has never been too accurate and more than a nice to watch animation but what about the progress indicator? Somewhere on the web, I have read that it’s been inaccurate, too. Well, be it so but at least you had some hint at all on how long defrag will take. Now you’re left with ‘disk defragmentation may take from few minutes to various hours’. What the h***!!! A SIMPLE PROGRESS BAR WOULD HAVE DONE THE JOB! How can having no idea at all on the duration be any better than an inaccurate prediction?

4) This one’s again about progress indicators. Now in the case of copying files. I thought the times of ’3772653665387752 minutes left’ predictions till copying files is done were gone since Windows XP. Windows 95, 98 and ME had this problem. Not that I really missed it in XP but now I guess, I should thank Windows Vista for reviving it!!!!!!

long copy

But besides that little bug Microsoft has tried hard to improve the file copy dialog. Now shows the transfer rate and waits and usually makes quite accurate predictions on the duration. But I don’t quite get how Microsoft does calculate it. Must be some numeric approximization of a really complex 5th order integral equation! Why? Because it usually takes 30 to 40 seconds, sometimes even longer till Vista gives you that number after starting to copy files. Till then you can read ‘Duration: calculating…’. This is annoying as many file transfers are even done by the time Windows has finished calculating the duration but it’s even worse for the fact, that after just a few seconds, Vista shows you a stable and accurate transfer rate in the copy dialog together with an also accurate size and number of files that are being copied. By that time, you or a eight year old child can calculate (just using your head, not even a calculator) the duration of the transfer within three to four seconds, sometimes even faster. This way, you have the result 30 to 40 seconds before Windows Vista does! That’s probably the worst thing at all in Windows Vista. Mostly because I just can’t understand, even after thinking about that really really hard, how and why Microsoft does that!

Anyone explain it to me? Please!

PS: Number 1) and 4) have been resolved by now. 1) by some Windows Update months ago, and 4) by Vista Service Pack 1 which has been available as a Release Candidate for a few weeks now.

January 18, 2008#

Firefox vs. Internet Explorer

No, that’s not gonna be some ‘you should use Firefox because it’s better and faster and safer and…’-article. In fact, I don’t even like seeing those words on official or semi-official Firefox banners. I’ve been an IE user for years until five years ago I switched to Firefox and fell in love with it. I definitely prefer Firefox over IE but for various reasons and with ‘faster’ and ‘safer’ NOT being among them. Basically I just like it for the many extensions there are and a few details I really enjoy.

Anyway, few days ago, I came across an icon while surfing the web, which I just wanted to show.


Though it might look this way, I just want to point out: I really don’t have any problem with IE. It’s a nice piece of software, too, and who ever likes it is free to do so! However, I like the idea of that icon! Close to the official one at first sight but with that but with those pearly white little details… ehh… and that blue ‘e’ ;-)  Just a creative, nice piece of art!

Oh… and by the way, that’s the usual icon you see on the web (with a little less salt and pepper):


January 18, 2008#

Homepage Update

As you can see, there’s gonna be an update to my website. This is mainly to simplify things a lot and use only one CMS, namely WordPress. I still have to figure out whether it does the job or not but I’m pretty positive on that.

Till migration is complete, you can still visit my old website at http://www.juergentreml.de/joomla/.

Update 2008-01-28:

Migration is complete. The old website has been taken offline and is no longer available.