C#: Ensure User only Runs One Copy of a Program

C#: Ensure User only Runs One Copy of a Program

Using a mutex, you can ensure that multiple users can run a program, but each one can run only one copy of the program.

This can come in handy when users are logging into a Citrix server or other shared server, and you don't want to limit the application to running only once.

The following code illustrates this concept:

01.using System;
02.using System.Security.Principal;
03.using System.Threading;
04.using System.Windows.Forms;
05. 
06.namespace WindowsFormsApplication2
07.{
08.    static class Program
09.    {
10.        [STAThread]
11.        static void Main()
12.        {
13.            bool ok;
14.            string strMyAppName = "ObscureAppName";
15.            string strMutex = WindowsIdentity.GetCurrent().Name.ToString();
16.            strMutex = strMutex.Split('\\')[1];
17.            strMutex += strMyAppName;
18.            Mutex m = new Mutex(true, strMutex, out  ok);
19.            if (!ok)
20.            {
21.                MessageBox.Show("Already running");
22.                Application.Exit();
23.            }
24.            if (ok)
25.            {
26.                Application.Run(new Form1());
27.                GC.KeepAlive(m);                // important!
28.            }
29.        }
30.    }
31.}

This is an extension of the work previously done by Michael A. Covington here: http://www.ai.uga.edu/mc/SingleInstance.html
Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Karl Mitschke edited Revision 7. Comment: Found the link for the original code I had modified.

  • Karl Mitschke edited Revision 6. Comment: Modified to have an application name, and added line numbers

  • Karl Mitschke edited Revision 3. Comment: Modified code to show on one line

  • Karl Mitschke edited Revision 4. Comment: Broke the code

  • Naomi  N edited Revision 1. Comment: Title case, more tags

Page 1 of 1 (5 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Karl Mitschke edited Revision 7. Comment: Found the link for the original code I had modified.

  • Karl Mitschke edited Revision 6. Comment: Modified to have an application name, and added line numbers

  • Again, thanks for your feedback.

    I have modified the code to prevent such an occurrence.

    Karl

  • Hi,

    Here no other application runs,,, it is not just about the copy of the same application ..  meaning if you change the strUser then other apps run with the above check in the code ,, To generalize,, a user with the above code can run only one app ,,, hence other apps having the same pattern can't run

    Regards.  

  • Thanks for your feedback.

    I have verified that simply renaming the program will not allow it to run multiple copies.

    Karl

  • Actually this method is applicable at a premitive level,,for a better approach there should be something like a registry entry check for a running copy,,,  else simply renaming the program would let it run in multiples ..... and there may be lots of other ways....

  • Karl Mitschke edited Revision 3. Comment: Modified code to show on one line

  • Karl Mitschke edited Revision 4. Comment: Broke the code

  • Naomi  N edited Revision 1. Comment: Title case, more tags

Page 1 of 1 (9 items)