C # Winfo Determine whether the current program has started or exists in several ways

Source: Internet
Author: User

The first way: The mutex mutex is implemented with only one instance of the process running

  

 Static class Program {///<summary>//The main entry point of the application. </summary> [STAThread] static void Main () {Application.enablevisualstyles (            );            Application.setcompatibletextrenderingdefault (FALSE); Control the current program has been opened (that is, started)//mode 1: Using mutex mutex implementation at the same time only one process instance running//mutex (mutex)//mutex is a mutually exclusive synchronization object, which means that at the same time there is only             There is one thread that can get it.            Mutexes can be applied when a shared resource can only be accessed by one thread at a time bool flag = FALSE;            System.Threading.Mutex Hmutex = new System.Threading.Mutex (True, Application.productname, out flag);            BOOL B = Hmutex.waitone (0, false); /* Above parameter description: The first parameter "initiallyowned": true: Indicates whether the calling thread should have the initial ownership of the mutex (honestly not understood) the second parameter "name": Program unique Name, (in the current operating system              The third parameter, "Creatednew", that determines the flag that runs repeatedly: Returns a value that returns (FALSE) if it detects that it has been started.            */if (flag) {//does not start the same program Application.Run (new MainForm ()); } else {MessageBox.Show ("The current program is already running, do not run repeatedly.")                "); Environment.exit (1);//Exit Program}}}

  

C # Winfo Determine whether the current program has started or exists in several ways

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.