Only one instance of the application can be run on the same PC (considering multiple user sessions)

Source: Internet
Author: User

Original: Only one application instance can be run on the same PC (considering multiple user sessions)

1 class Program2 {3     Private StaticMutex m;4 5 [STAThread]6     Static voidMain ()7     {8         BOOLCreateNew =false;9 Ten         /* One * on servers running Terminal Services, named system mutexes can have two levels of visibility.  A * If the name begins with the prefix "global\", the mutex is visible in all Terminal Server sessions.  - * If the name begins with the prefix "local\", the mutex is only visible in the Terminal Server session in which it was created. In this case, you can have a separate mutex with the same name in each of the other Terminal server sessions on the server.  - * If you do not specify a prefix when creating a named mutex, it takes the prefix "local\".  the * In a Terminal Server session, only two mutexes with different name prefixes are separate mutexes, and these two mutexes are visible to all processes in a Terminal Server session.  - * That is, the prefix name "global\" and "local\" describe the scope of the mutex name relative to the Terminal Server session (not the process).  -          */ -         stringMutexname = String.Format ("global\\{0}","IPP PCL"); +  -         Try  +         { Am =NewSystem.Threading.Mutex (true, Mutexname, outcreateNew); at         } -         Catch(unauthorizedaccessexception unauthorizedaccessex) -         { -             //A named mutex exists and has access control security, but the user does not have mutexrights.fullcontrol.  -         } -  in         if(!CreateNew) -         { toConsole.WriteLine ("application is running"); +  -             return; the         } *  $Console.WriteLine ("Success");Panax Notoginseng  - Console.readkey (); the     } +}

Only one instance of the application can be run on the same PC (considering multiple user sessions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.