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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:34:23+00:00 2026-05-13T14:34:23+00:00

I have a method to process the rows from a datatable using multiple threads,

  • 0

I have a method to process the rows from a datatable using multiple threads, it queues up all the work items and then checks that they have all been processed, not leaving the method until they have.

It seems to work fine in development, but when I put in onto the server (64-bit) for testing, it will not wait at the end of the method. It doesn’t even seem to do the Thread.Sleep() calls, as the method exits straight away.

It will continue to process the datarows after leaving the method, but that’s not what I want.

Any ideas?
Thanks

Public Sub ProcessAll(ByVal collection As DataTable, ByVal processDelegate As WaitCallback)
        Dim workItem As DataRow
        Dim availableThreads As Integer
        Dim completionPortThreads As Integer


        ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS)

        ' loop round processing each pending record adding them to the Thread Queue
        For Each workItem In collection.Rows
            ThreadPool.QueueUserWorkItem(processDelegate, workItem)
        Next

        ' The ThreadPool is a collection of background threads, thus we need to do something to stop the main thread from moving on
        Do
            Thread.Sleep(1000)
            ThreadPool.GetAvailableThreads(availableThreads, completionPortThreads)
            ' in the case that all threads are free (how we check all are complete) wait a few seconds just to make sure
            If availableThreads = MAX_THREADS Then
                Thread.Sleep(5000)
                ThreadPool.GetAvailableThreads(availableThreads, completionPortThreads)
            End If
        Loop While availableThreads < MAX_THREADS
End Sub
  • 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-13T14:34:23+00:00Added an answer on May 13, 2026 at 2:34 pm

    You shouldn’t be doing it this way to wait until all the rows have been completed.

    You should instead use a notification method that will, from your processing code, tell some piece of code that “hey, I just finished a row”.

    This way, you can more easily keep track of how many rows have been processed.

    The way I would do it:

    1. Add an Int32 value, initialized to 0, which keeps track of how many have been processed
    2. Use Interlocked.Increment to increase this in your delegate
    3. Set an event object in your delegate, after increasing the counter
    4. In your main method, I would wait for the event to be set, and then inspect the counter. If it isn’t high enough, loop back and wait again.

    ie. something like:

    private volatile Int32 _Processed = 0;
    private AutoResetEvent _RowProcessedEvent = new AutoResetEvent(false);
    
    ... in your delegate:
    Interlocked.Increment(ref _Processed);
    _RowProcessedEvent.Set();
    
    ... in your main method:
    while (_Processed < collection.Rows.Count)
    {
        _RowProcessedEvent.WaitOne(Timeout.Infinite);
    }
    

    don’t forget to close the event object when you’re done with it.

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

Sidebar

Ask A Question

Stats

  • Questions 455k
  • Answers 455k
  • 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 do not need to add any protocols, the documentation… May 15, 2026 at 10:03 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure what is the reason for the exception… May 15, 2026 at 10:03 pm
  • Editorial Team
    Editorial Team added an answer I had a look at the source code of jslint,… May 15, 2026 at 10:03 pm

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.