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

The Archive Base Latest Questions

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

I have a collection which has elements to be processed and at max only

  • 0

I have a collection which has elements to be processed and at max only four elements can be processed together. At run time all process started together and all goes in waiting state. Only four elements are processed at one time.

Problem is the processing elements are chosen randomly as all threads are waiting for resources to get free. Means first element can be the last one from collection.

But, I need to process elements in order they are in the collection.

Please tell me how can I achieve this?

I am using TPL and C# 4.0

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

    This is how I acheived this task

    public delegate void ProcessFinished(IParallelProcess process);
    public interface IParallelProcess
    {
        void Start();
        event ProcessFinished ProcessFinished;
    }
    
    public class ParallelProcessBasket : ConcurrentQueue<IParallelProcess>
    {
        public void Put(IParallelProcess process)
        {
            base.Enqueue(process);
        }
        public IParallelProcess Get()
        {
            IParallelProcess process = null;
            base.TryDequeue(out process);
            return process;
        }
    }
    public class ParallelProcessor<T> where T : class
    {
        private ParallelProcessBasket basket;
        private readonly int MAX_DEGREE_OF_PARALLELISM;
        private Action<T> action;
        public ParallelProcessor(int degreeOfParallelism, IEnumerable<IParallelProcess> processes, Action<T> action)
        {
            basket = new ParallelProcessBasket();
            this.action = action;
            processes.ToList().ForEach(
                (p) =>
                {
                    basket.Enqueue(p);
                    p.ProcessFinished += new ProcessFinished(p_ProcessFinished);
                });
            MAX_DEGREE_OF_PARALLELISM = degreeOfParallelism;
        }
    
        private void p_ProcessFinished(IParallelProcess process)
        {
            if (!basket.IsEmpty)
            {
                T element = basket.Get() as T;
                if (element != null)
                {
                    Task.Factory.StartNew(() => action(element));
                }
            }
        }
    
    
        public void StartProcessing()
        {
            // take first level of iteration
            for (int cnt = 0; cnt < MAX_DEGREE_OF_PARALLELISM; cnt++)
            {
                if (!basket.IsEmpty)
                {
                    T element = basket.Get() as T;
                    if (element != null)
                    {
                        Task.Factory.StartNew(() => action(element));
                    }
                }
            }
        }
    }
    static void Main(string[] args)    
    {
         ParallelProcessor<ParallelTask> pr = new ParallelProcessor<ParallelTask>(Environment.ProcessorCount, collection, (e) => e.Method1());
                pr.StartProcessing();
    }
    

    Thanks..

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

Sidebar

Related Questions

I have a list: Collection users which has around 100K+ records of users (all
I have a mongo collection which has documents with two fields fieldA and fieldB,
I have a small data collection app which has a single button to start
I have a Customer object which has a collection of ContactNumbers. Is it possible
I have a Person class which has a String collection of aliases representing additional
I have an APVendor class, which has a collection of APInvoice classes and a
How do I save a large collection with NHibernate which has elements that surpass
I have an class which has a collection, mapped as a bag in my
A map does binary search on all its elements, which has logarithmic complexity —
Let's say I have a large collection of elements. Each element has a position

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.