Only one process can be run, and processes can be run

Source: Internet
Author: User

Only one process can be run, and processes can be run

// Copyright (c) // All rights reserved.////Describe : Only can run single program.//Author   : Qing//Date     : 2014-08-14#ifndef __SINGLE_PROGRAM_H__#define __SINGLE_PROGRAM_H__#include <string>#include <Windows.h>namespace Qing{    class SingleProgram    {    public:        SingleProgram(void) : m_Mutex(NULL)        {        }        ~SingleProgram(void)        {            MandatoryDestoryInstance();        }        bool CreateSingleInstance(const std::string &ProgramName)        {            m_Mutex = ::CreateMutexA(NULL, TRUE, ProgramName.c_str());            DWORD LastError =::GetLastError();            if(m_Mutex != NULL)            {                ::ReleaseMutex(m_Mutex);            }            if(LastError == ERROR_ALREADY_EXISTS)            {                m_Mutex = NULL;                return false;            }            return true;        }        void MandatoryDestoryInstance()        {            if(m_Mutex != NULL)            {                ::CloseHandle(m_Mutex);                m_Mutex = NULL;            }        }    private:        SingleProgram(const SingleProgram&);        SingleProgram& operator=(const SingleProgram&);    private:        HANDLE m_Mutex;    };}#endif

 

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.