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

  • Home
  • SEARCH
  • 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 8050169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:59:39+00:00 2026-06-05T06:59:39+00:00

I have a console programm that runs different commands. I want to be able

  • 0

I have a console programm that runs different commands. I want to be able to run multiple instances of the programm in parallel however, I want to prevent specific commands from beeing processed in parallel.

I thought of a simple file lock system.

Given command “foo” is not allowed to be run in parallel, will that work?

  1. before running foo check if file .lock_foo_* exists (if so, abort)
  2. if not, create file .lock_foo_GUID
  3. check if .lock_foo_GUID exists and if no other .lock_foo_* file exists

if there is another .lock_foo_* file, that means that another instance created one at the simultanously and we abort and remove our lock file.

I wonder if there are any race conditions and if that’s the correct way to handle it?

UPDATE

I think it’s actually easier than initially expected (at least on windows). I think the answer from thb set me on the right track.

I think something like this should work

public bool CreateLock(string fileName){
     try{
         File.Open(string.Format(".lock_{0}",fileName), FileMode.Create, FileAccess.ReadWrite, FileShare.None);
      }
      catch (Exception exception){
           return false;
      }
      return true;
}

The OS will make sure that the file will be only created if it doesn’t exist yet and will otherwise raise an exception. So this method should be safe. Unfortunatly this will not work on UNIX though.

  • 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. Editorial Team
    Editorial Team
    2026-06-05T06:59:41+00:00Added an answer on June 5, 2026 at 6:59 am

    Your logic looks pretty good. However, if it interests you, there already exist more than one command-line utility that do what you want, for example lockfile and flock.

    Try man 1 lockfile. I suspect that that manpage will succinctly answer your question.

    On the other hand, if you need or prefer to implement this, yourself, then the approach you suggest is almost right but must sporadically, very occasionally fail. This is frustrating, but is inherent to the kind of thing you are trying to do. The reason is that a brief interval interposes between (1) checking whether another process has locked the resource and (2) locking the resource. What happens if, during this brief interval, another process should lock the resource?

    What is needed is an atomic operation, which somehow checks and locks as a single, uninterruptible step. Fortunately, modern CPUs provide such atomic operations and modern operating-system kernels expose them, precisely so you can indeed reliably do what you want.

    If this interests you, then you can consult Microsoft’s development docs for LockFile and LockFileEx.

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

Sidebar

Related Questions

I have a simple .bat script that runs an external console program and when
I have a windows form that creates multiple console applications of the same program
Hey everyone. I have an exe file that runs a console program that was
I have a console application that runs some temperamental hardware. If I don't detach
We have a console app that runs in .NET 3.5. It connects to a
I have a simple x86-targeting .NET 3.5 console program that calls methods on COM
I have a C console program in VS2010 that I would like to edit/debug/build
I have the windows console program code below that should show up NT Authoroty\Network
I have program, that must interact with a console program before my program can
I have one application that prints messages from Test.exe in console .My java program

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.