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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:02:53+00:00 2026-05-27T05:02:53+00:00

Is it possible (in a meaningful way) ? For example, say I’d like to

  • 0

Is it possible (in a meaningful way) ?

For example, say I’d like to implement a waitable queue as follows:

public class WaitableQueue : WaitHandle {
    public WaitableQueue() {
      q = new Queue();
    }

    public void Enqueue(Object Arg) {
      lock(this) {
        q.Enqueue(Arg);
        // set the waithandle here (how?)
      }
    }

    public Type Dequeue() {
      lock(this) {
        if(q.Count == 1)
        // reset the waithandle here (how?)
        return q.Dequeue();
      }
    }

    Queue q;
  }
  • 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-27T05:02:54+00:00Added an answer on May 27, 2026 at 5:02 am

    The important thing to remember is that you must set the SafeWaitHandle property.

    From MSDN:

    When you derive from WaitHandle, use the SafeWaitHandle property to
    store your native handle operating system handle. You do not need to
    override the protected Dispose method unless you use additional
    unmanaged resources.

    Here is how I would do this.

    public class QueueWaitHandle<T> : WaitHandle
    {
        private Queue<T> queue = new Queue<T>();
        private ManualResetEvent signal = new ManualResetEvent(false);
    
        public QueueWaitHandle()
        {
            base.SafeWaitHandle = signal.SafeWaitHandle;
        }
    
        public void Enqueue(T item)
        {
            lock (queue)
            {
                queue.Enqueue(item);
                signal.Set();
            }
        }
    
        public T Dequeue()
        {
            lock (queue)
            {
                T item = queue.Dequeue();
                if (queue.Count == 0)
                {
                    signal.Reset();
                }
                return item;
            }
        }
    }
    

    When done this way Enqueue acts like the Set method and Dequeue acts like the Reset method. So basically this works like a counting event where non-zero is signaled and zero is unsignaled. The queue is doing the counting in this case and it just happens to also hold data.

    I know you were asking about subclassing WaitHandle in general, but this specific data structure is more than just an exercise. It can be useful in some scenarios. I would not, however, call it a waitable queue because that implies, at least to me anyway, that the Dequeue operation will block when the queue is empty. Clearly that is not what will happen in this particular implemenation.

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

Sidebar

Related Questions

Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In
class Parent { public: void func1(); // Complete meaningful definition in parent given. virtual
Currently, I have something like this:- public class MyHolder<T> { private T value; public
Possible Duplicate: Is there any way to detect strings like putjbtghguhjjjanika? php check if
I have a class where one of the public methods is a perfect fit
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
Possible Duplicate: How to do a meaningful code-coverage analysis of my unit-tests? Is there
I would like to know if it is possible in an SQL query to
I'm looking for the best way to make it possible to get a random

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.