2

I want to open up an existing instance if my program is already running only if its running the same version. I noticed that this question was asked for only if the name exists, but what if the version is older, I just want to notify the user that an older version is still running, "please remove older version before starting this version."

The other link is this one: Return to an already open application when a user tries to open a new instance but they don't talk about closing an instance if an older or newer version is detected.

Community
  • 1
  • 1
RetroCoder
  • 2,597
  • 10
  • 52
  • 81

3 Answers3

3

You can use a variant of the method which I describe here and that described here using System.Threading.Mutex.

I guess you could have two mutex's. Mutex A named by a GUID and mutex B named by GUID and version number.

Your new instance can first check for mutex A on start up. If mutex A does not exist, then no other version is running. If mutex A exists then some other version is running.

The new instance can then check for existence of mutex B, using it's own version number. If B does exist, then the other instance is the same version as the new instance, if B doesn't exist, then some other version is running.

Community
  • 1
  • 1
Adam Ralph
  • 29,453
  • 4
  • 60
  • 67
  • Could you give a code example? I have an open source project that I need to detect if a previous version is already running or detect if its the current or later release and do different things. – RetroCoder Nov 29 '11 at 18:55
  • 1
    Have a look at the second link (http://sanity-free.org/143/csharp_dotnet_single_instance_application.html). It has code examples of how to use a mutex. – Adam Ralph Nov 30 '11 at 18:13
1

What about making your application a singleton, thereby restricting it to a single instance.

Sujay Ghosh
  • 2,828
  • 8
  • 30
  • 47
  • 1
    The OP states that richer behaviour is required, i.e. the new instance should simply delegate to the current instance if the versions are the same, but give a notification regarding versioning when the versions are different. – Adam Ralph Nov 27 '11 at 15:20
1

If an older version is already deployed then you may not be able to change the code in it. In which case you can simply get the currently running processes and look for the exe name of your older version app and then investigate the version number. If its an older version then you can do whatever you want.

If you want to build this functionality for future then you can create a namedpipe and a new running app will try to connect to that pipe to get the latest version.

Community
  • 1
  • 1
Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131