I have a vb.net service that needs some threads to handle each function call separately and avoid time consumption.
I have 2 functions that needs to implement threads :
Before asking my questions, here's my two functions :
1- LaunchTasks() : If the task is well launched it calculates the 'NextRunDate' for this task otherwise it skips it.
'Dim oThread As Threading.Thread
For Each oRow As DataRow In oDatatable.Rows
Dim oCLASSE_Task As New CLASSE_Task
If oCLASSE_Task.Load(oRow.Item("Mytask")) Then
'oThread = New Threading.Thread(AddressOf oTask.launchSteps)
'oThread.Priority = Threading.ThreadPriority.Normal
'oThread.Start()
Dim oThread = System.Threading.Tasks.Task(Of Boolean).Factory.StartNew(Function() oCLASSE_Task.launchSteps())
End If
Next
' Need to wait until all threads finish
2- calculateRunDates()
'It's called for each object in CLASSE_Task
'Dim oThread As Threading.Thread
For Each oRow As DataRow In oDatatable.Rows
Dim oCLASSE_Task As New CLASSE_Task
If oCLASSE_Task.Load(oRow.Item("Mytask")) Then
Dim oThreadTask = System.Threading.Tasks.Task(Of Boolean).Factory.StartNew(Function() oCLASSE_Task.calculateNextRunDate())
'oThread = New Threading.Thread(AddressOf oTask.calculateNextRunDate)
'oThread.Priority = Threading.ThreadPriority.Normal
'oThread.Start()
End If
Next
' Need to wait until all threads finish
I always call LaunchTask() first and then calculateRunDates() (if I get new record in the database).
I must wait for all the threads in the function LaunchTask() to finish before I start the calculateRunDates().
How can I do this using TPL (I never used it before), something like thread.join()?
Should I use threads or TPL in this situation?
How Can I handle exception using TPL, in a service (somthing like that)?
Note: I have locks in my CLASSE_Task using SyncLock.
There is not enough examples using vb.net, mostly C#.
Hope that I was clear enough.