I have an application that takes too long to run, and I want to enter threading / parallelization / whatever.
In particular, the code returns several thousand letters, and then sends them. Today, the code looks like this (a bit simplified):
Dim mails = centreInteretService.GetEmails()
For Each m in mails
m.Body = GetMailContent(m)
If MailSendable(m) Then
SendMail(m)
End If
Next
I want to try sending several letters in parallel. I would like to try with two threads in parallel. More specifically, I would like to put the whole loop in a stream (getmailcontent + sendmail).
I thought of something like this:
Dim mails1 As New List(Of MailSerialiserCI)
Dim mails2 As New List(Of MailSerialiserCI)
Dim nbFirstList As Integer = CInt(Math.Ceiling(nbTotal / 2))
mails1 = mails.Take(nbFirstList)
mails2 = mails.Skip(nbFirstList)
Dim smt1 As New MailSender.MailSenderThreaded()
smt1.mails = mails1
smt1.nbTotal = nbTotal
Dim threadMails1 As ThreadStart = New ThreadStart(AddressOf smt1.SendMails)
Dim th1 As Thread = New Thread(AddressOf threadMails1)
th1.Start()
Dim smt2 As New MailSender.MailSenderThreaded()
smt2.mails = mails2
smt2.nbTotal = nbTotal
Dim threadMails2 As ThreadStart = New ThreadStart(AddressOf smt2.SendMails)
Dim th2 As Thread = New Thread(AddressOf threadMails2)
th2.Start()
And MailSenderThreaded looks like this:
Public Class MailSenderThreaded
Public mails As List(Of MailSerialiserCI)
Public nbTotal As Integer
Public Sub SendMails()
LoopMails(Me.mails, Me.nbTotal)
End Sub
End Class
But strings New Thread(AdressOf x)give me an error: no applicable function x matching delegate System.Threading.ParameterizedThreadStart.
, , , , ; ; .NET 4, .NET 3.5...
, ?