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 668669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:03:58+00:00 2026-05-14T00:03:58+00:00

I have a common resource, which I want 1 and only 1 instance of

  • 0

I have a common resource, which I want 1 and only 1 instance of my application (or it’s COM API) to have access to at any time. I have tried to protect this resource using mutexes, but when multiple threads of a host dotnet application try to access the COM object, the mutex doesn’t seem to be released. This is the code I have used to protect my resource.

repeat
  Mutex := CreateMutex(nil, True, PChar('Connections'));
until (Mutex <> 0) and (GetLastError <> ERROR_ALREADY_EXISTS);
  try
    //use resource here!
  finally
    CloseHandle(Mutex);
  end;

If I run the threads simultaneously, the first thread get’s through (obviously, being the first one to create the mutex), but subsequent threads are caught in the repeat loop. If I run each thread at 5 second intervals, then all is ok.

I suspect I’m not using mutexes correctly here, but I have found very little documentation about how to do this.

Any ideas?

  • 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-05-14T00:03:59+00:00Added an answer on May 14, 2026 at 12:03 am

    You’re using the mutex wrong. You should be waiting for it and releasing it, not recreating it constantly.

    During initialization:

    Mutex := CreateMutex(nil, False, 'Connections');
    if Mutex = 0 then
      RaiseLastOSError;
    

    When you want to access the resource

    if WaitForSingleObject(Mutex, INFINITE) <> WAIT_OBJECT_0 then
      RaiseLastOSError;
    try
      // Use resource here
    finally
      ReleaseMutex(Mutex)
    end;
    

    During finalization

    CloseHandle(Mutex);
    

    Also, since mutexes are global, you should pick something a little more unique than “connections” for the name. We added a GUID to the end of ours.

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

Sidebar

Related Questions

I want to create templates for base new reports on to have common designs.
We have several common libs. Ideally we want them all to use the latest
This has to be a common question that all programmers have from time to
i have several common elements (components), that will generate some html. it seems my
We have a common problem of moving our development SQL 2005 database onto shared
I have a common code of serializing a class object in my 3-4 methods
Do you have a common base class for Hibernate entities, i.e. a MappedSuperclass with
I have a project where there are multiple applications that have some common configuration
I would love to be able to have a common set of Classes in
It's common to have a table where for example the the fields are account,

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.