Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 48627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:18:35+00:00 2026-05-10T16:18:35+00:00

I need to make sure that user can run only one instance of my

  • 0

I need to make sure that user can run only one instance of my program at a time.
Which means, that I have to check programatically, whether the same program is already running, and quit in such case.

The first thing that came to my mind was to create a file somewhere, when the program starts. Then, each other instance of the program would check for this file and exit if it found it.
The trouble is, that the program must always exit gracefully and be able to delete the file it created, for this to work. In case of, say, power outage, the lock file remains in place and the program can’t be started again.

To solve this, I decided to store the first program’s process ID into the lock file and when another instance starts, it checks if the PID from the file is attached to some running process.
If the file doesn’t exist, is empty, or the PID doesn’t correspond to any existing process, the program continues to run and writes its own PID to the file.

This seems to work quite fine – even after an unexpected shutdown, the chance that the (now obsolete) process ID will be associated with some other program, seems to be quite low.

But it still doesn’t feel right (there is a chance of getting locked by some unrelated process) and working with process IDs seems to go beyond the standard C++ and probably isn’t very portable either.

So, is there another (more clean and secure) way of doing this? Ideally one that would work with the ISO 98 C++ standard and on Windows and *nix alike.
If it cannot be done platform-independently, Linux/Unix is a priority for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:18:35+00:00Added an answer on May 10, 2026 at 4:18 pm

    There are several methods you can use to accomplish only allowing one instance of your application:

    Method 1: Global synchronization object or memory

    It’s usually done by creating a named global mutex or event. If it is already created, then you know the program is already running.

    For example in windows you could do:

        #define APPLICATION_INSTANCE_MUTEX_NAME '{BA49C45E-B29A-4359-A07C-51B65B5571AD}'      //Make sure at most one instance of the tool is running     HANDLE hMutexOneInstance(::CreateMutex( NULL, TRUE, APPLICATION_INSTANCE_MUTEX_NAME));     bool bAlreadyRunning((::GetLastError() == ERROR_ALREADY_EXISTS));     if (hMutexOneInstance == NULL || bAlreadyRunning)     {         if(hMutexOneInstance)         {             ::ReleaseMutex(hMutexOneInstance);             ::CloseHandle(hMutexOneInstance);         }         throw std::exception('The application is already running');     } 

    Method 2: Locking a file, second program can’t open the file, so it’s open

    You could also exclusively open a file by locking it on application open. If the file is already exclusively opened, and your application cannot receive a file handle, then that means the program is already running. On windows you’d simply not specify sharing flags FILE_SHARE_WRITE on the file you’re opening with CreateFile API. On linux you’d use flock.

    Method 3: Search for process name:

    You could enumerate the active processes and search for one with your process name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate a user's screen name to make sure that it can
In my C#/MVC4/Razor Application I need to make sure that user enter not only
I have method in a class that I need to make sure is only
I have a requirement, where we need to make sure that the document [PDF]
I have two Ajax calls. I need to make sure that a JS function
For a project, we need a way to run user scripts that can come
I need to make sure that the height of the header of table is
We need to make sure only results within the last 30 days are returned
I have a task which I need to run in the background in my
I have service that I need to run every 5 minutes. I already have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.