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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:52:56+00:00 2026-05-22T11:52:56+00:00

I need to develop an app that is using multithreading. Basicly, I have a

  • 0

I need to develop an app that is using multithreading.

Basicly, I have a DataTable that contains around 200k rows.
From each row, I need to take a field, compare it to a webpage,
and then remove it from the datatable.

The thing is, the server serving those pages has a limit on concurrent requests.
so at max I can ask for 3 pages at the same time.

I want to do this by using the threadpool,
I even managed building a simple app that does that ( locks the datatable )
but I couldn’t limit the concurrent threads ( even with SetMaxThreads ) it seems like it just ignored the limit.

does anyone have something ready made that does something similar ?
I would love to see.

i have tried using semaphores, but got into problems:

        static SemaphoreSlim _sem = new SemaphoreSlim(3);    // Capacity of 3
    static List<string> records = new List<string>();

    static void Main()
    {
        records.Add("aaa");
        records.Add("bbb");
        records.Add("ccc");
        records.Add("ddd");
        records.Add("eee");
        records.Add("fff");
        records.Add("ggg");
        records.Add("iii");
        records.Add("jjj");

        for (int i = 0; i < records.Count; i++ )
        {
            new Thread(ThreadJob).Start(records[i]);
        }

        Console.WriteLine(records.Count);
        Console.ReadLine();
    }

    static void ThreadJob(object id)
    {
        Console.WriteLine(id + " wants to enter");
        _sem.Wait();
        Console.WriteLine(id + " is in!");           // Only three threads
        //Thread.Sleep(1000 * (int)id);               // can be here at
        Console.WriteLine(id + " is leaving");       // a time.

        lock (records)
        {
            records.Remove((string)id);
        }

        _sem.Release();
    }

this runs quite nicely, the only problem is,

Console.WriteLine(records.count);

returns diffrent results.
even due i understand that it happens since not all the threads have finished ( an i a m calling the records.count before all records have been removed) i couldnt find how to wait for all to finish.

  • 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-22T11:52:57+00:00Added an answer on May 22, 2026 at 11:52 am

    To wait for multiple threads to finish, you can use multiple EventWaitHandle‘s and then call WaitHandle.WaitAll to block the main thread until all events are signalled:

    // we need to keep a list of synchronization events
    var finishEvents = new List<EventWaitHandle>();
    
    for (int i = 0; i < records.Count; i++ )
    {
        // for each job, create an event and add it to the list
        var signal = new EventWaitHandle(false, EventResetMode.ManualReset);
        finishEvents.Add(signal);
    
        // we need to catch the id in a separate variable
        // for the closure to work as expected
        var id = records[i];
    
        var thread = new Thread(() =>
            {
                // do the job
                ThreadJob(id);
    
                // signal the main thread
                signal.Set();
            });
    }
    
    WaitHandle.WaitAll(finishEvents.ToArray());
    

    Since most of these threads would end up suspended most of the time, it would be better to use ThreadPool in this case, so you can replace new Thread with:

        ThreadPool.QueueUserWorkItem(s =>
        {
            ThreadJob(id);
            signal.Set();
        });
    

    When you are done with the events, don’t forget to Dispose them:

    foreach (var evt in finishEvents)
    {
        evt.Dispose();
    }
    

    [Edit]

    To put it all in one place, here is what your example code should look like:

    static Semaphore _sem = new Semaphore(3, 3);    // Capacity of 3
    static List<string> _records = new List<string>(new string[] { "aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh" });
    
    static void Main()
    {
        var finishEvents = new List<EventWaitHandle>();
    
        for (int i = 0; i < _records.Count; i++)
        {
            var signal = new EventWaitHandle(false, EventResetMode.ManualReset);
            finishEvents.Add(signal);
    
            var id = _records[i];
            var t = new Thread(() =>
            {
                ThreadJob(id);
                signal.Set();
            });
    
            t.Start();
        }
    
        WaitHandle.WaitAll(finishEvents.ToArray());
    
        Console.WriteLine(_records.Count);
        Console.ReadLine();
    }
    
    static void ThreadJob(object id)
    {
        Console.WriteLine(id + " wants to enter");
        _sem.WaitOne();
    
        Console.WriteLine(id + " is in!");
        Thread.Sleep(1000);
        Console.WriteLine(id + " is leaving");
    
        lock (_records)
        {
            _records.Remove((string)id);
        }
    
        _sem.Release();
    }
    

    (note that I’ve used Semaphore instead of SemaphoreSlim because I don’t have .NET 4 on this machine and I wanted to test the code before updating the answer)

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

Sidebar

Related Questions

I need to develop a CRM system which will allow users to have a
I need to develop a process that will detect if the users computer has
In the project I am currently working on, we have the need to develop
I'm using Sqlite with android to develop and app which can be customizable (for
I am trying to develop a document based mac app using this Apple walkthrough
I need to develop a small-medium sized desktop GUI application, preferably with Python as
I need to develop a generic jQuery-based search plugin for the ASP.NET MVC application
I need to develop a system for storing large numbers (10's to 100's of
I need to develop an application which stores data in a SQL Server 2005
Consider the need to develop a lightweight desktop DB application on the Microsoft platforms.

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.