Private Function ProcessAllClients() As Integer
Dim clientList As New List(Of String)
Dim exceptionMessages As New List(Of String)
Dim totalNoOfRecProcessed As Integer = 0
'Add some Test Clients
clientList.Add("A1")
clientList.Add("A2")
clientList.Add("A3")
If clientList IsNot Nothing AndAlso clientList.Count > 0 Then
'Customize No of Threads you may want to use as per need or you can simply use a value
Dim noOfThreadCounts As Integer = MaxAllowableThreadCount
Dim noOfClients As Integer = clientList.Count
Dim noOfThreadsGroup As Integer = 0
'Adjust No of Loops as per max allowable thread
If (noOfClients Mod noOfThreadCounts = 0) Then
noOfThreadsGroup = CInt(Math.Floor(noOfClients / noOfThreadCounts))
Else
noOfThreadsGroup = CInt(Math.Floor(noOfClients / noOfThreadCounts)) + 1
End If
'Set clients in each group/loop
For threadGroup As Integer = 0 To noOfThreadsGroup - 1
Dim processingTaskCounter As Integer = noOfThreadCounts
If threadGroup = (noOfThreadsGroup - 1) AndAlso (noOfClients Mod noOfThreadCounts > 0) Then
processingTaskCounter = noOfClients Mod noOfThreadCounts
End If
Dim allProcessingTasks(processingTaskCounter - 1) As Task(Of Integer)
Dim clinetsToProcess As New List(Of String)
Dim taskCounter As Integer = 0
For i = 0 To processingTaskCounter - 1
clinetsToProcess.Add(clientList(CInt(threadGroup * noOfThreadCounts) + i))
Next
For Each clientCode As String In clinetsToProcess
Try
Dim currentClient As String = clientCode
allProcessingTasks(taskCounter) = New Task(Of Integer)(Function()
Dim pHelper As New ProcessHelper
Return pHelper.ClientProcessingLogic(currentClient)
End Function)
taskCounter = taskCounter + 1
Catch ex As Exception
exceptionMessages.Add("Sorry! Error Occurred." & ex.Message)
totalNoOfRecProcessed = 0
End Try
Next
'Now Execute all client's tasks as once but in seperate thread
Array.ForEach(allProcessingTasks, Sub(tx) tx.Start())
'Wait till all finishes processing
Task.WaitAll(allProcessingTasks)
For Each tsk In allProcessingTasks
Select Case True
Case tsk.IsCanceled
exceptionMessages.Add("Task ID" & tsk.Id & " was canceled.")
Case tsk.IsFaulted
exceptionMessages.Add("Task ID" & tsk.Id & " was faulted.")
Case tsk.IsCompleted
totalNoOfRecProcessed += tsk.Result
Case Else
exceptionMessages.Add("Task ID" & tsk.Id & " was in unknown status.")
End Select
Next
Next
End If
Return totalNoOfRecProcessed
End Function