0

不明な番号に電子メールの招待状を送信しようとしています。スレッド化を使用して、速度の点でプロセスを少し改善できることを望んでいました。私がしたことがまったく役に立たないか、まったく役に立たないかどうか、誰かアドバイスをお願いします。ありがとう。

   Public Shared Function Emailing_FriendInvitation(ByVal memberID As String _
                                   , ByVal referrerCode As String _
                                   , ByVal enquiryCode As String _
                                   , ByVal outletCode As String _
                                   , ByVal templateName As String _
                                   , ByVal emailAddressLists As List(Of String) _
                                   , ByVal dbtrans As DbTransaction _
                                   , ByVal dbConnection As DbConnection _
                                   , ByVal addedBy As String _
                                   , ByVal customerCode As String) As Boolean
        Dim dbCommand As DbCommand = Nothing
        Dim isSuccess As Boolean = False
        Try
            Dim senderEmailName As String = String.Empty
            Dim senderEmail As String = String.Empty
            Dim receipientEmail As String = String.Empty

            Dim receipientName As String = "Dear Sir/Madam"
            Dim emailSubject As String = String.Empty
            Dim emailBody As String = String.Empty

            Dim ds As DataSet = Nothing

            If dbConnection.State <> ConnectionState.Open Then
                dbConnection.Open()
            End If
            dbCommand = GetStoredProcedureCommand("FriendsInvitation")
            dbCommand.Connection = dbConnection
            If Not dbtrans Is Nothing Then
                dbCommand.Transaction = dbtrans
            End If

            Dim emailAddresses As String
            For Each email In emailAddressLists
                emailAddresses += email + ","
            Next

            AddInParameter(dbCommand, "@MemberID", DbType.String, memberID)
            AddInParameter(dbCommand, "@EmailAddresses", DbType.String, emailAddresses)
            AddInParameter(dbCommand, "@TemplateName", DbType.String, 256, templateName)
            AddInParameter(dbCommand, "@EnquiryCode", DbType.String, 50, enquiryCode)
            AddInParameter(dbCommand, "@OutletCode", DbType.String, 50, outletCode)
            AddInParameter(dbCommand, "@ReferrerCode", DbType.String, 50, referrerCode)
            AddInParameter(dbCommand, "@AddedBy", DbType.String, 50, addedBy)

            ds = ExecuteDataSet(dbCommand)
            senderEmailName = Convert.ToString(IIf(Not IsDBNull(ds.Tables(0).Rows(0)("AutoEmailDisplayName")), ds.Tables(0).Rows(0)("AutoEmailDisplayName"), String.Empty))
            senderEmail = Convert.ToString(IIf(Not IsDBNull(ds.Tables(0).Rows(0)("AutoEmailSender")), ds.Tables(0).Rows(0)("AutoEmailSender"), String.Empty))
            emailSubject = templateName
            emailBody = Convert.ToString(IIf(Not IsDBNull(ds.Tables(0).Rows(0)("AutoEmailContent")), ds.Tables(0).Rows(0)("AutoEmailContent"), String.Empty))
            'emailBlastRecipientAutoID = Convert.ToString(ds.Tables(1).Rows(0)("EmailBlastRecipientAutoID"))
            ' End If
            If Not (String.IsNullOrEmpty(senderEmail) AndAlso String.IsNullOrEmpty(receipientEmail) AndAlso _
              String.IsNullOrEmpty(emailSubject) AndAlso String.IsNullOrEmpty(emailBody)) Then

      emailBody = emailBody.Replace("[REFERRERCODE]", "REFERRERCODE")

      '----Thread-----

      Dim Th = New Thread(Sub() sendEmail(emailAddressLists, senderEmailName, senderEmail,     emailSubject, emailBody))
                Th.Start()
      End If

      '---------------

        Catch ex As Exception
            isSuccess = False
            Throw New DALException(ex, dbCommand, customerCode, "Emailing_FriendInvitation")
        Finally
            If Not dbCommand Is Nothing Then
                dbCommand.Dispose()
            End If
        End Try
        Return isSuccess
    End Function

    Private Shared Sub sendEmail(ByVal emailLst As List(Of String), ByVal senderEmailName As String, ByVal senderEmail As String, ByVal emailSubject As String, ByVal emailBody As String)
        Dim successfulReceipents As String = String.Empty

        For Each emailAdd In emailLst
            Try
                Email.Send(senderEmailName, senderEmail, emailAdd, emailSubject, emailBody, Nothing)
                successfulReceipents += emailAdd + ","
            Catch ex As Exception

            End Try
        Next
    End Sub
4

1 に答える 1

2

いいえ、並行して作業を行っていないため、これは役に立ちません。作業をバックグラウンド スレッドに置いているだけです。これにより、UI がブロックされないようにすることができますが、実行速度はまったく向上しません。

sendEmail適切な方法は、サブの For ループ内でスレッドを生成することです。しかし、そうしないでください。これは実際にはスレッド化の良い候補ではありません。

于 2012-06-08T02:42:59.390 に答える