Allow only one instance of a Windows CE application to run at a time

Jon Skeet posted a helpful tip on How do I make sure that only one instance of my application runs at a time?. If you are running a .NET Application and are looking for how to force Windows to allow only one instance of your application to run at anytime, then I recommend you follow Skeet’s advice.

However, if you looking for a solution on how to force one-instance of your application that is targeting the .NET Compact Framework, Skeet’s post will be helpful conceptually, but the code will not work for you because of the fact that the CF version of the System.Threading.Mutex class doesn’t support the overload Mutex(bool, string, out bool).

Here’s a screenshot of the Mutex overload that Skeet uses in his example:

To work-around the limitations of CF, you can accomplish the goal by getting your hands dirty with a native call (P/Invoke) to CreateMutex(). But who’s got time for that when you can just use the free Smart Device Framework from the good guys over at OpenNETCF.

Here’s a snippet of how to make sure only one CF application is running at a time using the SDF OpenNETCF.Threading.NamedMutex() class:

    7     static class Program

    8     {

    9         /// <summary>

   10         /// The main entry point for the application.

   11         /// </summary>

   12         [MTAThread]

   13         static void Main()

   14         {

   15             bool firstInstance;

   16             //Instantiate the Mutex and give it a GUID name

   17             OpenNETCF.Threading.NamedMutex mutex =

   18                 new OpenNETCF.Threading.NamedMutex

   19                 (false,

   20                 "{50A9DA83-979B-4b6c-A095-DD1880F2B181}",

   21                 out firstInstance);


   23             //If another thread has entered the Main()

   24             //entry point, notify the user, then bail out.

   25             if (!firstInstance)

   26             {

   27                 MessageBox.Show("Form1 is already running");

   28                 return;

   29             }


   31             //Instantiate the Form application then show it

   32             Application.Run(new Form1());



   35             GC.KeepAlive(mutex);

   36         }

   37     }

Regarding the Garbage Collector call at

   35 GC.KeepAlive(mutex);

– Jon Skeet’s reasoning on this is

One thing to beware of is that the mutex isn’t garbage collected. If a local variable is only used near the start of a method, the GC may ignore it when working out which variables are garbage collection “roots” if that part of the method has already been executed. This can lead to the mutex being released earlier than you might anticipate! To prevent this from happening, make a call to GC.KeepAlive(mutex); at the end of your main method. [ref]

How to Programmatically Start Internet Explorer on Windows CE

To start Internet Explorer in code (C# in this case), you will use the System.Diagnostics.Process class.

System.Diagnostics.Process.Start("IESample.exe", "");

Notice that the name of the IE executable on Windows CE is “IESample.exe“.

Persist Data On CUWIN5500

You have developed an application for the CUWIN5500 and installed the application in a directory within the Root – For example: “\Program Files\MyApp”. However, your directory (i.e. “…\MyApp” and all of its contents goes away every time you boot the device.

How do you get the data to persist?

On the CUWIN, there are two ways to persist data –

  1. Adding information to the Registry and then saving the Registry via the “SaveRegs” application (located at “\Windows\SaveRegs”)
  2. Saving the data to the “\Flash Disk” (or any external media such as the SD card). When saving data to the Flash Disk, the CUWIN automatically persists that data.

At the time of this writing, I don’t know how to persist the data via the Registry method. I will update this post if I ever get around to it. The fact of the matter is, in my case, saving my data to the Flash Disk (and SD card) is all I need so I really have no motivation to learn the Registry method.

AutoRun Application on CUWIN5500 Windows CE Device

You built an application (.exe) for a CUWIN5500 and now you want the application to start automatically after the device boots up.

  1. On the CUWIN, go to “\Flash Disk” and then create a folder named “AutoRun”
  2. Move your application (.exe) to the AutoRun directory
  3. Turn on the CUWIN dip switch #2 (Dip switches #1 and #2 should be ON and all dip switches set to OFF)
  4. Reboot the CUWIN device
  5. Your application should automatically run

ASP Executing VBScript Crashes Windows CE Device

I am experiencing strange behavior in my .ASP web page that is running on Windows CE (CUWIN5500 device).

The entire HTML/Script is this…

		<title>Loop Test</title>
	<h3>WinCE Crashes After X-Amount of Looping</h3>
        for i=0 to 250

For an unknown reason, the code above will (sometimes) crash the entire OS – specifically, the device freezes up to the point you cannot even move the mouse around. The only way to rectify the freeze is to reboot the device.

Additionally, and what makes the problem even more curious, if I append something else to the “i” index variable, the system will NOT crash/freeze up the device. For example, the update below DOES NOT crash the device…

		<title>Loop Test</title>
	<h3>WinCE Crashes After X-Amount of Looping</h3>	
        for i=0 to 250
        response.Write(i & "<br />")

I do not know what is causing this problem to occur and I log it here in this post for future reference.