Methods for implementing mutually exclusive program running

Source: Internet
Author: User
ArticleDirectory
    • Implemented using the compiler (using the MFC Framework)
    • Use named mutex objects

In Win32ProgramThere are many mutex running methods. I will analyze several different implementations below:

Implementation in VC

Visual c ++ is the most important and powerful programming tool for Win32 programming. To cite a name, VC is "what you can't think of, what VC can't do ". To put it bluntly, let's take a look at how VC works.

Implemented using the compiler (using the MFC Framework)

Add the following lines at the beginning of the. cpp file:

 
# Pragma data_seg ("shared") int volatile g_lappinstance = 0; # pragma data_seg ()

Let's take a look at the above content:

    • The first sentence # pragma data_seg ("shared") creates a new section called shared.
    • Int volatile g_lappinstance = 0: Put g_lappinstance in the shared section. Note that the compiler will put g_lappinstance into the shared section only when g_lappinstance is initialized. Otherwise, it will be placed in a section other than shared. (In fact, the visual C ++ compiler provides an allocate specifier that allows us to put data in any section .)
    • The third sentence indicates that the compiler shared section ends.
 
# Pragma comment (linker, "/section: shared, RWS ")

In this sentence, the compilation linker knows that our shared section has read, write, and share attributes. This is the key to implementing mutex operations. In this way, the g_lappinstance variable can be shared among multiple instances of the application. Add the following to the initinstance () function:

 
If (++ g_lappinstance> 1) {afxmessagebox ("the program is running! "); Return false ;}

AboveCodeThe function is to add 1 to g_lappinstancd in the program. If the value is found to be greater than 1, the message box is displayed and false is returned. (Note: In MFC, if initinstance returns false, the program will not be run !)

Use named mutex objects

Using the API function createmutex to create a named mutex object to implement program mutex is a common method. We can add the following code to the ininininstance () function:

 
Handle hobject = createmutex (null, false, "ljpxyxc"); If (getlasterror () = error_already_exists) {closehandle (hobject); afxmessagebox ("the program is already running! "); Return false ;}

The above createmutext function creates a mutex object named "ljpxyxc". When the second instance of the program is run, createmutex is called to return error_already_exists. We can use this value to implement mutually exclusive program running.

VB implementation

In programs implemented by VB, The Global Object app. preinstance can implement this function. App. preinstance is a Boolean value. If the value is true, it indicates that an instance of the program is running. If the value is false, the program can run.

We can program the loading event of the form to implement this function. The Code is as follows:

 
Private sub formateload () If app. preinstance = true thenmsgbox "the program is running !" Unload meend ifend sub
Implementation of Delphi and C ++ Builder

I have not found that the compilers of Delphi and C ++ builder have the same functions as VC. So we can call createmutex to implement this function. For details about the functions createmutext () and getlasterror (), refer to msdn.

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.