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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:30:48+00:00 2026-05-16T22:30:48+00:00

I have a library with an API much like this: public class Library :

  • 0

I have a library with an API much like this:

public class Library : IDisposable
{
    public Library(Action callback);
    public string Query();
    public void Dispose();
}

After I have instantiated Library, at any time and on any thread it might invoke the callback I have passed to it. That callback needs to call Query to do useful work. The library will only stop calling my callback once disposed, but if a callback attempts to call Query after the main thread has called Dispose, bad stuff will happen.

I do want to allow the callbacks to run on multiple threads simultaneously. That’s okay. But we need to be sure that no callbacks can be running when we call Dispose. I thought a ReaderWriterLockSlim might be appropriate – you need the write-lock to call Dispose, and the callbacks need read-locks to call Query. The problem here is that ReaderWriterLockSlim is IDisposable, and I don’t think it will ever be safe to dispose it – I can never know that there is not a callback in flight that simply hasn’t gotten to the point of acquiring the read-lock yet.

What should I do? It looks like ReaderWriterLock isn’t IDisposable, but it’s own documentation says you should use ReaderWriterLockSlim instead. I could try to do something equivalent with just the “lock” keyword, but that sounds wasteful and easy to screw up.

PS – Feel free to say that the library API is not good if you think that’s the case. I would personally prefer that it guaranteed that Dispose would block until all callbacks had completed.

  • 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-16T22:30:49+00:00Added an answer on May 16, 2026 at 10:30 pm

    This sounds like something you can wrap with your own API which makes the guarantee from the final paragraph.

    Essentially, each callback should atomically register that it’s running, and check whether it’s still okay to run – and then either quit immediately (equivalent to never being called) or do its stuff and deregister that it’s running.

    Your Dispose method just needs to block until it finds a time when nothing’s running, atomically checking whether anything’s running and invalidating if not.

    I can imagine this being done reasonably simply using a simple lock, monitor, Wait/Pulse approach. Your API wrapper would wrap any callback it’s given inside another callback which does all this, so you only need to put the logic in one place.

    Do you see what I mean? I don’t have time to implement it for you right now, but I can elaborate on the ideas if you like.

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

Sidebar

Related Questions

I have a library with an Interface. Public Interface Progress { int ProgressValue{get;set;}, string
When designing a C API for configuring a library/utility, I have a co-worker who
I have a library written in C++, and a wrapper for this library written
I have a library written in C that I would like to use in
I have an class library called ServiceLayer which acts as a repository for a
I am working on making a loupe tool using C#. Much like this one:
I'm making an iPad app and I have this image: I'd like offer users
I have a library made in C++/GDI that I am trying to port to
I have a library project that should be the base for several web applications.
We have a library created as both .lib and .dll (it's a big library

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.