Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't ThreadPool.GetAvailableThreads working?
    text
    copied!<p>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.</p> <p>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.</p> <p>It will continue to process the datarows after leaving the method, but that's not what I want.</p> <p>Any ideas? Thanks</p> <pre><code>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 &lt; MAX_THREADS End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload