여러 스레드를 사용하여 데이터 테이블의 행을 처리하는 메서드가 있고 모든 작업 항목을 대기열에 넣은 다음 메서드가 끝날 때까지 메서드가 끝나지 않은 채 모든 처리가 완료되었는지 확인합니다.ThreadPool.GetAvailableThreads가 작동하지 않는 이유는 무엇입니까?
개발 과정에서 문제가 없지만 테스트를 위해 서버 (64 비트)에 넣을 때 메서드의 끝에서 기다리지 않습니다. 메서드가 곧바로 종료되기 때문에 Thread.Sleep() 호출도하지 않는 것입니다.
메소드를 종료 한 후에도 데이터row를 계속 처리하지만, 원하는 것은 아닙니다.
아이디어가 있으십니까? 감사
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
해결하려는 근본적인 문제는 무엇입니까? 예. * 왜 *이 코드가 있습니까? –
정말 간단합니다. 개별적으로 처리해야하는 DB의 엔티티 목록이 있으며 처리 속도가 매우 빨라야합니다. –