0

我想快速发送电子邮件,我使用线程池。但是,我计算了使用线程池发送十封电子邮件所花费的时间和发送十封电子邮件串行所花费的时间(在每个循环中)并且时间是相同的。我的代码有什么问题?

我的线程池代码:

private sub mymethod()
    addr.Add("first address")
    addr.Add("second address")
    addr.Add("third address")

    addr.Add("tenth address")


    ThreadPool.SetMaxThreads(20, 20)
    ThreadPool.SetMinThreads(15, 15)

    start = Date.Now.TimeOfDay.TotalSeconds
    For Each add As Object In addr

        ThreadPool.QueueUserWorkItem(AddressOf SendMail, add) 

    Next
   End Sub

     Private Sub SendMail(ByVal add As Object)
    If add Is Nothing Then
        Throw New ArgumentException("Empty arg!!")
    End If

    Dim client As SmtpClient = New SmtpClient()
    client.Host = "smtp.gmail.com"
    client.Port = "587"
    client.Credentials = New System.Net.NetworkCredential("address", "password")
    client.EnableSsl = True


    Using Mail As New MailMessage
        Mail.Subject = "test"
        Mail.To.Add(add)
        Mail.From = New MailAddress("address")
        Mail.Body = "body"
        client.ServicePoint.MaxIdleTime = 1 

        Try
            client.Send(Mail)
        Catch ex As SmtpException
        Finally
             count += 1
            SendCompleted() 
        End Try
    End Using

End Sub

 Private Sub SendCompleted()

    If Me.InvokeRequired Then
        Me.Invoke(New Action(AddressOf SendCompleted))
        Return
    End If

    If (count = addr.Count) Then

        end_ = Date.Now.TimeOfDay.TotalSeconds

        label1.Text = (end_ - start)
    End If


End Sub
4

0 回答 0