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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:21:19+00:00 2026-06-12T03:21:19+00:00

I am working with an interface which sits on top of (for example) a

  • 0

I am working with an interface which sits on top of (for example) a StreamReader or SqlDataReader. The interface exposes a method, GetNext(), which returns an object if there are any left, or null if there are none left.

public interface ICollectionWidget<T>
{
    T GetNext(); // Returns a T if there are any left, or null if there aren't
}

I need to process each T returned by GetNext() in parallel, and stop processing when GetNext() returns null. I’m not quite sure how this is done (using TPL or whatever). I need a kind of parallel while! Obviously, I don’t want any threads still processing to finish when I get a null, I just don’t want to add any new processing – and then to drop out of the ‘loop’ when all the threads finish what they’re doing.

Can anyone help? Please let me know if my question doesn’t make sense.

  • 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-06-12T03:21:20+00:00Added an answer on June 12, 2026 at 3:21 am

    Note that “collections” like the one you’re showing are typically expose via IEnumerable<T>. If you have control over the API itself, I’d use IEnumerable<T> instead of the GetNext()-based iteration approach. If you don’t, however, it’s simple to perform the conversion…

    I would wrap this API to expose it as an IEnumerable<T>. You could then use Parallel.ForEach:

    private IEnumerable<T> EnumerateWidgets<T>(ICollectionWidget<T> widgets)
    {
        T element = widgets.GetNext();
        while (element != null)
        {
            yield return element;
            element = widgets.GetNext();
        }
    }
    

    You could then use:

    Parallel.ForEach(EnumerateWidgets(widgetCollection), widget =>
    {
         // Process widget here
    });
    

    This will prevent threading issues while enumerating your widgets (as the enumerator will be single threaded), but allow you to process your collection in parallel.

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

Sidebar

Related Questions

I am working with a library which exposes an interface to work with. One
I am working on an interface which involves A LOT of javascript. There is
I am working on a custom interface for the JW Player which displays the
I'm working on an embedded systems which provides an interface with certain types. The
I'm working on implementing a hierarchy which deals with Comparator and the Comparable interface.
I'm working on some sort of online multiuser editor / coop interface, which will
I'm working on a graphical interface which connects a 8051 with the computer through
I'm working on a web interface which currently runs using PHP and communicates locally
I'm working on a fiddly web interface which is mostly built with JavaScript. Its
I'm working on a Linux based server system in which there are two network

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.