C # multithreaded Development 9: Use the mutex class to prevent an application from starting twice

Source: Internet
Author: User

You can use a mutex class to create a name-exclusive mutex, and the system can recognize a named mutex, so you can use the mutex class to prohibit the application from starting two times.

The following is the demo code for this scenario.

Using system;using system.threading;namespace run1instanceexample{    class program    {        [STAThread]        static void Main (string[] args)        {            bool creatednew;            var mutex = new Mutex (false, "Singletonapp", out creatednew);            if (!creatednew)            {                return;            }            else            {                Console.WriteLine ("Good job!");                Console.ReadLine ();}}}    

A mutex with 3 parameters is used in the code constructor to create a mutex object.

The 1th parameter specifies whether the mutex should initially be owned by the keynote thread;

The 2nd parameter defines the name of the mutex;

The 3rd parameter is an output parameter that receives a Boolean value that indicates whether the mutex is new. If False is returned, the mutex is already defined, and the application that created the mutex is started, the application cannot be started, or the application is started.

C # multithreaded Development 9: Use the mutex class to prevent an application from starting twice

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.