C # Check whether it is already running (two methods)

Source: Internet
Author: User

There are times when we need to run the software alone instead of repeating it for no reason.

Let me give you an example: Can you choose to run the music playback software repeatedly?

You have a right-click shortcut open in the registration form (REG), but if the soft

is simply meant to be repeated running instances presumably users see such a soft

The expression must be very exaggerated and amusing. But what about being serious?

Avoid multiple runs of software I give two ways to be a mutex (mutex)
The other is the API event two methods are better, you can understand

The above two methods will register a string flag in the system each time the software starts

To check if a flag exists, but software destruction means that the system will automatically

Release the flag so we don't need to go to close and CloseHandle and

I don't recommend it. To shut down the kernel object of the system who knows you will not because

Cause the program or system to be unstable for the operation no one can guarantee that, right?

Ease of use I'm feeling pretty good. very humane and not fancy technology is difficult.

Using System.Threading;

            BOOL Bcreatednew;            Mutex mutex = new Mutex (false, "MyApp", out bcreatednew);
All we need to do is to determine if Bcreatednew is false. If not

Create success or enter false otherwise true if we want to determine if the system is running

The same software and the system does not allow us to register the same flag then surely it will be false

, the reason is easy to say Tongqiao, right. Now let's talk about the API event approach

Using System.Runtime.InteropServices;

            const int NULL = 0;            const int event_all_access = 2031619;            if (OpenEvent (event_all_access, False, "MyApp"))            {                //running            }            else            {                //not running                CreateEvent (NULL, False, False, "MyApp");            }

        [DllImport ("kernel32.dll", SetLastError = True)]        public static extern bool OpenEvent (int dwdesiredaccess, bool binherithandle, string lpname);        [DllImport ("kernel32.dll", SetLastError = True)]        public static extern bool CreateEvent (int lpeventattribute, BOOL bManualReset, bool bInheritHandle, string lpname);



C # Checks whether it is already running (two methods)

Related Article

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.