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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:13:49+00:00 2026-05-16T09:13:49+00:00

copy paste the following code in new C# console app. class Program { static

  • 0

copy paste the following code in new C# console app.

class Program
{
    static void Main(string[] args)
    {
        var enumerator = new QueuedEnumerator<long>();
        var listenerWaitHandle = Listener(enumerator);

        Publisher(enumerator);
        listenerWaitHandle.WaitOne();
    }

    private static AutoResetEvent Listener(IEnumerator<long> items)
    {
        var @event = new AutoResetEvent(false);
        ThreadPool.QueueUserWorkItem((o) =>
        {
            while (items.MoveNext())
            {
                Console.WriteLine("Received : " + items.Current);
                Thread.Sleep(2 * 1000);
            }
            (o as AutoResetEvent).Set();
        }, @event);
        return @event;
    }

    private static void Publisher(QueuedEnumerator<long> enumerator)
    {
        for (int i = 0; i < 10; i++)
        {
            enumerator.Set(i);
            Console.WriteLine("Sended : " + i);
            Thread.Sleep(1 * 1000);
        }
        enumerator.Finish();
    }

    class QueuedEnumerator<T> : IEnumerator<T>
    {
        private Queue _internal = Queue.Synchronized(new Queue());
        private T _current;
        private bool _finished;
        private AutoResetEvent _setted = new AutoResetEvent(false);

        public void Finish()
        {
            _finished = true;
            _setted.Set();
        }

        public void Set(T item)
        {
            if (_internal.Count > 3)
            {
                Console.WriteLine("I'm full, give the listener some slack !");
                Thread.Sleep(3 * 1000);
                Set(item);
            }
            else
            {
                _internal.Enqueue(item);
                _setted.Set();
            }
        }

        public T Current
        {
            get { return _current; }
        }

        public void Dispose()
        {
        }


        object System.Collections.IEnumerator.Current
        {
            get { return _current; }
        }

        public bool MoveNext()
        {
            if (_finished && _internal.Count == 0)
                return false;
            else if (_internal.Count > 0)
            {
                _current = (T)_internal.Dequeue();
                return true;
            }
            else
            {
                _setted.WaitOne();
                return MoveNext();
            }
        }

        public void Reset()
        {
        }
    }
}

2 threads (A,B)

A thread can provide one instance at a time and calls the Set method
B thread wants to receive a sequence of instances (provided by thread A)

So literally transforming an Add(item), Add(item), .. to a IEnumerable between different threads

Other solutions also welcome of course!

  • 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-16T09:13:50+00:00Added an answer on May 16, 2026 at 9:13 am

    Sure – this code might not be the best way to do it, but here was my initial stab at it:

    Subject<Item> toAddObservable;
    ListObservable<Item> buffer;
    
    void Init()
    {
        // Subjects are an IObservable we can trigger by-hand, they're the 
        // mutable variables of Rx
        toAddObservable = new Subject(Scheduler.TaskPool);
    
        // ListObservable will hold all our items until someone asks for them
        // It will yield exactly *one* item, but only when toAddObservable
        // is completed.
        buffer = new ListObservable<Item>(toAddObservable);
    }
    
    void Add(Item to_add)
    {
        lock (this) {
            // Subjects themselves are thread-safe, but we still need the lock
            // to protect against the reset in FetchResults
            ToAddOnAnotherThread.OnNext(to_add);
        }
    }
    
    IEnumerable<Item> FetchResults()
    {
        IEnumerable<Item> ret = null;
        buffer.Subscribe(x => ret = x);
    
        lock (this) {
            toAddObservable.OnCompleted();
            Init();     // Recreate everything
        }
    
        return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you copy and paste the following text in a html page, &#1575;&#1606;&#1608;&#1575;&#1606; you
I am following their tutorial here: http://developers.sones.de/wiki/doku.php?id=quickreference:fiveminuteguide But when I copy and paste this
i have such code (copy paste from wiki). Its multiplication of those big numbers
I use UIPasteboard to copy/paste text between two UITextView . Code looks like this:
I am not a fan of boilerplate code: copy-paste reuse is potentially error-prone. Even
I have the following code: import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; public final class
I'm new to javascript/jquery. I have the following basic code and I keep receiving
I have the following code copy/pasted multiple times. The values that change are the
in order to avoid copy/paste, i can use a unique view for different actions,
We need to stop copy paste of few characters like '<' or '>' or

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.