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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:28:46+00:00 2026-05-13T06:28:46+00:00

I have written a com component in .NET and if I try to take

  • 0

I have written a com component in .NET and if I try to take a lock on any object in any method (that is invoked by unmanaged code talking to my com component) I get an exception.

I do not have the exact text of the exception at this moment but it wasn’t much helpful either.

So my question is under what circumstances a lock(syncObject) would throw an exception?
Here are some facts:

  • syncObject is not null
  • syncObject is not already locked

Would it have anything to do with callee running in STA (Single Threaded Apartment) or MTA (Multi Threaded Apartment)?

  • 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-13T06:28:46+00:00Added an answer on May 13, 2026 at 6:28 am

    From this page:

    Every lock acquisition might throw an exception. Be prepared for it.

    Most locks lazily allocate an event if a lock acquisition encounters contention, including CLR monitors. This allocation can fail during low resource conditions, causing OOMs originating from the entrance to the lock. (Note that a typical non-blocking spin lock cannot fail with OOM, which allows it to be used in some resource constrained scenarios such as inside a CER.) Similarly, a host like SQL Server can perform deadlock detection and even break those deadlocks by generating exceptions that originate from the Enter statement, manifesting as a System.Runtime.InteropServices.COMException.

    Often there isn’t much that can be done in response to such an exception. But reliability- and security-sensitive code that must deal with failure robustly should consider this case. We would have liked it to be the case that host deadlocks can be responded to intelligently, but most library code can’t intelligently unwind to a safe point on the stack so that it can back-off and retry the operation. There is simply too much cross-library intermixing on a typical stack. This is why timeout-based Monitor acquisitions with TryEnter are typically a bad idea for deadlock prevention.

    So as you can read, it seems that under resource constrained conditions we might get exceptions thrown from the Enter method of Monitor which lock(o) uses internally.

    So perhaps your solution is something like a spin-wait?

    {
        uint iters = 0;
        while (!cond) {
            if ((++iters % 50) == 0) {
                // Every so often we sleep with a 1ms timeout (see #30 for justification).
                Thread.Sleep(1);
            } else if (Environment.ProcessorCount == 1) {
                // On a single-CPU machine we yield the thread.
                Thread.Sleep(0);
            } else {
                // Issue YIELD instructions to let the other hardware thread move.
                Thread.SpinWait(25);
            }
        }
    }
    

    Where cond could be some

    private volatile int cond = 0
    

    used with e.g. Interlocked.CompareExchange where you change to e.g. Thread.Current.ManagedThreadID or something else non-zero?

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

Sidebar

Related Questions

I have a COM component written in C# that makes calls to ConfigurationManager.AppSettings.Get(...). This
I have written a COM object that in turn uses a thrid party ActiveX
I have a Windows Service written in .NET C# which calls a COM component.
I have a COM component written in VB6 that I'm using from a Visual
I have a COM component written in C++ that has a Print function. This
I have written a COM component in unmanaged C++ to provide customers access to
I have a COM component that was originally written in Visual Studio 6. It
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have an application written in PHP that uses a COM dll written in
I have a COM outproc server written in ATL that registers itself using _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,

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.