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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:32:22+00:00 2026-05-11T02:32:22+00:00

I’ve created a custom thread pool utility, but there seems to be a problem

  • 0

I’ve created a custom thread pool utility, but there seems to be a problem that I cannot find.

using System; using System.Collections; using System.Collections.Generic; using System.Threading;  namespace iWallpaper.S3Uploader { public class QueueManager<T> {     private readonly Queue queue = Queue.Synchronized(new Queue());     private readonly AutoResetEvent res = new AutoResetEvent(true);     private readonly AutoResetEvent res_thr = new AutoResetEvent(true);     private readonly Semaphore sem = new Semaphore(1, 4);     private readonly Thread thread;     private Action<T> DoWork;     private int Num_Of_Threads;      private QueueManager()     {         Num_Of_Threads = 0;         maxThread = 5;         thread = new Thread(Worker) {Name = 'S3Uploader EventRegisterer'};         thread.Start();          //   log.Info(String.Format('{0} [QUEUE] FileUploadQueueManager created', DateTime.Now.ToLongTimeString()));     }      public int maxThread { get; set; }      public static FileUploadQueueManager<T> Instance     {         get { return Nested.instance; }     }      /// <summary>     /// Executes multythreaded operation under items     /// </summary>     /// <param name='list'>List of items to proceed</param>     /// <param name='action'>Action under item</param>     /// <param name='MaxThreads'>Maximum threads</param>     public void Execute(IEnumerable<T> list, Action<T> action, int MaxThreads)     {         maxThread = MaxThreads;         DoWork = action;         foreach (T item in list)         {             Add(item);         }     }     public void ExecuteNoThread(IEnumerable<T> list, Action<T> action)     {         ExecuteNoThread(list, action, 0);     }     public void ExecuteNoThread(IEnumerable<T> list, Action<T> action, int MaxThreads)     {         foreach (T wallpaper in list)         {             action(wallpaper);         }     }     /// <summary>     /// Default 10 threads     /// </summary>     /// <param name='list'></param>     /// <param name='action'></param>     public void Execute(IEnumerable<T> list, Action<T> action)     {         Execute(list, action, 10);     }      private void Add(T item)     {         lock (queue)         {             queue.Enqueue(item);         }         res.Set();     }      private void Worker()     {         while (true)         {             if (queue.Count == 0)             {                 res.WaitOne();             }              if (Num_Of_Threads < maxThread)             {                 var t = new Thread(Proceed);                 t.Start();             }             else             {                 res_thr.WaitOne();             }         }     }      private void Proceed()     {         Interlocked.Increment(ref Num_Of_Threads);         if (queue.Count > 0)         {             var item = (T) queue.Dequeue();              sem.WaitOne();             ProceedItem(item);             sem.Release();         }         res_thr.Set();         Interlocked.Decrement(ref Num_Of_Threads);     }      private void ProceedItem(T activity)     {         if (DoWork != null)             DoWork(activity);          lock (Instance)         {             Console.Title = string.Format('ThrId:{0}/{4}, {1}, Activity({2} left):{3}',                                           thread.ManagedThreadId, DateTime.Now, queue.Count, activity,                                           Num_Of_Threads);         }     }      #region Nested type: Nested      protected class Nested     {         // Explicit static constructor to tell C# compiler         // not to mark type as beforefieldinit         internal static readonly QueueManager<T> instance = new FileUploadQueueManager<T>();     }      #endregion  } 

}

Problem is here:

Console.Title = string.Format('ThrId:{0}/{4}, {1}, Activity({2} left):{3}',                                       thread.ManagedThreadId, DateTime.Now, queue.Count, activity,                                       Num_Of_Threads); 

There is always ONE thread id in title. And program seems to be working in one thread.

Sample usage:

        var i_list = new int[] {1, 2, 4, 5, 6, 7, 8, 6};         QueueManager<int>.Instance.Execute(i_list,           i =>           {               Console.WriteLine('Some action under element number {0}', i);            }, 5); 

P.S.: it’s pretty messy, but I’m still working on it.

  • 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. 2026-05-11T02:32:23+00:00Added an answer on May 11, 2026 at 2:32 am

    Writing robust threaded code is not trivial. There are numerous thread-pools around that you might look at for reference, but also note that Parallel Extensions (available as CTP, or later in .NET 4.0) includes a lot of additional threading constructs out-of-the-box (in the TPL/CCR). For example, Parallel.For / Parallel.ForEach, which deal with work-stealing, and handling the available cores effectively.

    For an example of a pre-rolled thread-pool, see Jon Skeet’s CustomThreadPool here.

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

Sidebar

Ask A Question

Stats

  • Questions 116k
  • Answers 116k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need a width or height on the content for… May 11, 2026 at 10:34 pm
  • Editorial Team
    Editorial Team added an answer Perhaps this can help you with some of the functions/operators:… May 11, 2026 at 10:34 pm
  • Editorial Team
    Editorial Team added an answer Does it give you any meaningful error message if you… May 11, 2026 at 10:34 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.