The joy of the first glory with photography

I’ve been photographing for long. However, I was reluctant to participate in any type of photography related contests. For better or worse, things have changed now. My first-ever participation brings me lots of confidence and a little bit of pride. Two of my pictures have won the 108th and 160th positions in one of the dpreview challenges. This may not be a significant achievement but it is really encouraging given the fact that there were a total of 192 entries to the contest. See my entries here and here.

Posted in Photography | 1 Comment

Running one and only one instance of an application in C#.

Often, you’ll want to prevent users from starting more than one instance of an application at a time. This could be to avoid confusion, avoid data loss or may be it doesn’t make sense to run more than a single instance at a time. Whatever be the reason, it is easy to do this in C#. As far as I know, there are 2 popular approaches that programmers use these days : the process table approach and the named Mutex approach. The process table approach is slow, error-prone and it requires more lines of code when compared to the other. We discuss the named Mutex approach here.

Mutex

A Mutex is one of the many synchronization constructs available in C#. Mutexes are mutually exclusive objects – one and only one thread can acquire it at a time. The Mutex is different from the familiar Monitor object in that its scope could be system-wide. This means that a Mutex created in one running application can be accessed by another application provided the Mutex is a named Mutex and that it is not acquired by the first application. This allows inter-process synchronization of threads. In C#, the Mutex is represented by the System.Threading.Mutex class. Following are four most commonly used constructors to create a Mutex :

public Mutex()
public Mutex(Boolean initiallyOwned)
public Mutex(Boolean initiallyOwned, String name)
public Mutex(Boolean initiallyOwned, String name, out Boolean whatHappened)

The first constructor simply creates a Mutex which is not initially owned.

The second version of the constructor requires a Boolean argument that indicates whether the calling thread should have initial ownership of the Mutex. If initiallyOwned is true, the calling thread will own the Mutex. If initiallyOwned is false, the Mutex is unowned.

The third version is more interesting – it allows you to name the Mutex. It’s this name that we use to access the Mutex from another process.

The fourth version adds another Boolean argument – whatHappened – to the third version. This out (uninitialized) argument will tell you if your request to own the Mutex was granted or not. If whatHappened is true, your request was honored and that the calling thread was granted the ownership of the Mutex. If whatHappened is false, it indicates that someone else is holding the Mutex now. It is this information that we use to determine if another instance of the application is running or not.

There’s yet another version of the constructor that allows you to specify the access control security to be applied to the named Mutex. It is not discussed here. Let’s jump to the program now :

using System;
using System.Threading;
using System.Windows.Forms;

namespace OnlyOne
{
    class OneAndOnlyOne
    {
        static String _mutexID = "a8b65a4f-9ffb-46fd-a432-bdd3338c423e";
        static void Main(string[] args)
        {
            Boolean _isNotRunning;
            using (Mutex _mutex = new Mutex(true, _mutexID, out _isNotRunning))
            {
                if (_isNotRunning)
                {
                    MessageBox.Show("This is the first instance.");
                    Console.WriteLine("Press any key to continue...");
                    Console.Read();
                }
                else
                {
                    MessageBox.Show("An instance is already running.");
                    return;
                }
            }
        }
    }
}

As you can see, a named Mutex is in action here. The name of the Mutex should be unique enough to avoid any interference with other Mutex objects in other applications running on your system. We pass in an out parameter to the constructor which we will later use to determine the outcome of our request to own the Mutex. Here, if the Mutex is already acquired by some other thread in another instance of the application, this request will be rejected and the value of _isNotRunning will be false. So, we simply return after showing an error message. On the other hand, if the calling thread manages to acquire the Mutex, _isNotRunning will be true. In this case, we know that this is the one and only running instance of this application and we continue as usual.

There are few things to keep in mind from the above program. First, as we mentioned already, make sure that the name of the Mutex is unique. Using a GUID is one way to do it. You can also use the assembly name qualified by the namespace if it is unique enough to distinguish it from others. The second thing to notice is the scope of the Mutex. Consider the below code fragment from a Windows Forms Application :

using System;
using System.Windows.Forms;
using System.Threading;

namespace OneAndOnlyOne
{
    static class Program
    {
        static String _mutexID = "a8b65a4f-9ffb-46fd-a432-bdd3338c423e";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Boolean _isNotRunning;
            Mutex _mutex = new Mutex(true, _mutexID, out _isNotRunning);
            if (_isNotRunning)
            {
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("An instance is already running.");
                return;
            }
        }
    }
}

Do you see any problem here? Well, don’t worry if you don’t because the problem is not quite obvious. Consider a scenario where this program has been running for a while. Since the Mutex is not used after the form starts running, the garbage collector is free to wipe it out of memory. At this point, we can easily start another instance of the application because the new instance can create a new Mutex with the same old name. One way to prevent this is to tell the GC to keep the Mutex alive as long as the application runs. In code, we call the GC.KeepAlive(Object obj) method. The purpose of the KeepAlive method is to ensure the existence of a reference to an object that is at risk of being prematurely reclaimed by the garbage collector. With the additional call to GC, the code now looks like this :

using System;
using System.Windows.Forms;
using System.Threading;

namespace OneAndOnlyOne
{
    static class Program
    {
        static String _mutexID = "a8b65a4f-9ffb-46fd-a432-bdd3338c423e";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Boolean _isNotRunning;
            Mutex _mutex = new Mutex(true, _mutexID, out _isNotRunning);
            if (_isNotRunning)
            {
                Application.Run(new Form1());
                GC.KeepAlive(_mutex);
            }
            else
            {
                MessageBox.Show("An instance is already running.");
                return;
            }
        }
    }
}

Since Mutex derives from WaitHandle and WaitHandle implements IDisposable, a better approach would be to define the scope of the Mutex using the ‘using’ construct available in C#. This will avoid the extra call to GC and dispose the Mutex properly when finished as shown below :

using System;
using System.Windows.Forms;
using System.Threading;

namespace OneAndOnlyOne
{
    static class Program
    {
        static String _mutexID = "a8b65a4f-9ffb-46fd-a432-bdd3338c423e";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Boolean _isNotRunning;
            using (Mutex _mutex = new Mutex(true, _mutexID, out _isNotRunning))
            {
                if (_isNotRunning)
                {
                    Application.Run(new Form1());
                }
                else
                {
                    MessageBox.Show("An instance is already running.");
                    return;
                }
            }
        }
    }
}
Posted in C# | 4 Comments

A Day With LINKSYS RangePlus Wireless Network Adapter

I’d been planning to buy a wireless network adapter for quite some time now. My Airtel modem has both Wi-Fi and Ethernet interfaces. Also, the telephone line running to my room had become a real inconvenience as it prevented me from closing my room’s door. Now that I’ve got more than enough reasons, I went to SP road in Bangalore and bought a Linksys RangePlus wireless adapter with USB interface. Cisco is a company I respect and I’ve heard only good things about their products.

 

 

 

 

 

 

After returning to my room, I was eager to set the device up and see it in action. The package was excellent with the great looking black USB adapter, a CD ROM with the setup files and  a decent quick installation guide. I just had a look behind the box and then came the shock – the device doesn’t support Windows 7 ! I’m using Windows 7 Enterprise 64-bit edition and this device supports only Windows XP and Windows Vista. How on earth a reputed company like Cisco can ignore the support for the best Windows ever? I was really disappointed. I should have gone through the software requirements before I purchased it.

After the initial disappointment, I thought I would give it a try anyway. So I started the setup process and proceeded to the device detection screen. Setup detected my device and went on with the installation. At this point, it showed me the spinning wheel and went for a toss. I thought I would have to three-finger to bring it back to control but I was wrong. After a few anxious minutes, setup reported an error that it couldn’t install the device properly. It also listed a few possible troubleshooting tips. I tried all of them but it was all in vain.

 

 

 

 

 

 

 

 

Finally, I decided to try my usual trick – asking Windows to configure it for me. I removed the setup disk, restarted Windows and then connected the device. Voila ! Windows detected the adapter and installed it in a jiffy. Now I wanted to see if it was detecting my network. Success again ! It detected 3 networks and one of them was mine. After keying in the password, I was online in seconds.

Having configured the device and got it up and running, I wanted to have a look at the Linksys website to see any possible solutions for my problem. Apparently, someone at Linksys (or some of their customers) had faced this issue and they had given a workaround which was more or less the same as what I had done except that I didn’t install any Linksys drivers. The device worked just fine with the default drivers in Windows.

Posted in PC Hardware | Leave a comment

Hello Windows Phone 7 !

I’m really inspired by Charles Petzold‘s Windows Phone 7 book so much so that I started developing apps without a second thought. Charles explains the process of application development using Silverlight in his lucid and beautiful pros in well over 1000 pages. I got my Hello World programs in both Silverlight and XNA up and running im hardly 15 minutes. The one below is the Silverlight version :

 

 

 

 

 

 

 

 

 

 

 

 

And the XNA version is in its native landscape mode :

 

 

 

 

 

 

 

This is how the programs show up in the list :

 

 

 

 

 

 

 

 

 

 

 

 

The best thing I love about Windows Phone 7 development is – you don’t have to spend even a penny to get started ! If you have Visual Studio 2010, you already meet the requirement. No Visual Studio? Don’t worry. Microsoft has added a new version of Visual Studio to its Express Editions stable that specifically targets Windows Phone 7 developers. You can download it here.

Posted in Windows Phone 7 | Leave a comment

Welcome Netlander !

Dileep Krishnan

I’m very excited to write my first serious weblog here. Most of my earlier attempts to create a decent presence on the web did not succeed despite the fact that I’ve been owning this site for many years now. I tried a pure HTML site with hand-written XHTML 1.0 (strict) first. But it was very difficult to manage. Asp.Net was my next experiment. However, that also didn’t last for long as I myself became a Windows programmer over time. The time required to spend to keep up with the pace of Asp.Net didn’t justify the results. Once again, I decided to move on to something else which would make my life easier. I tried both dasblog and BlogEngine.Net. This time the results were promising but I could not keep up that spirit for long . And now, almost a year after my last experiment, I find myself hooked to WordPress. My web hosting provider readily supports WordPress out of the box and that the setup was a breeze. This time, I really hope to start writing something that others would find worth taking a look at.

Posted in Welcome | Leave a comment