我的代码出了什么问题

本文关键字:什么 问题 代码 我的 | 更新日期: 2023-09-27 18:28:10

我的代码出了什么问题??我正在按钮点击事件中启动线程。我没有得到任何错误,但它不会激发发送电子邮件的子

Dim thread As New Thread(New ThreadStart(AddressOf sendEmail))
        thread.Start()
        thread.IsBackground = True
        thread.Priority = System.Threading.ThreadPriority.Normal
        Response.Redirect(getRedirectionPath)
 Public Sub sendEmail()
        Response.Redirect("~/Index")
    End Sub

为什么它不起作用?

更新:我收到错误"THREAD WAS BEING ABORTED"

更新:根据请求,我已经上传了整个代码片段

 Protected Sub btnLocalSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLocalSubmit.Click
        'Dim _User As MembershipUser = Membership.GetUser(_userName)
        'Dim _UserGUID As Object = _User.ProviderUserKey

        Dim _con As New SqlConnection(ConfigurationManager.ConnectionStrings("CitizenJDBConnectionString").ConnectionString)
        Dim _sqlComm As New SqlCommand("INSERT INTO LocalReport VALUES (@UserID,@ReportTitle,@Report,@State,@City,@Locality,'0','0',@Tags,@TimeStamp)", _con)
        Try
            _sqlComm.Parameters.Add("UserID", Data.SqlDbType.VarChar)
            _sqlComm.Parameters("UserID").Value = _userName
            _sqlComm.Parameters.Add("ReportTitle", Data.SqlDbType.VarChar)
            _sqlComm.Parameters("ReportTitle").Value = txtLocalReportTitle.Text
            _sqlComm.Parameters.Add("Report", Data.SqlDbType.VarChar)
            _sqlComm.Parameters("Report").Value = ckLocalReport.Text
            _sqlComm.Parameters.Add("State", Data.SqlDbType.VarChar)
            _sqlComm.Parameters("State").Value = ddlState.SelectedValue
            _sqlComm.Parameters.Add("City", Data.SqlDbType.VarChar)
            _sqlComm.Parameters("City").Value = ddlCity.SelectedItem.Value
            _sqlComm.Parameters.Add("Locality", Data.SqlDbType.VarChar)
            _sqlComm.Parameters("Locality").Value = txtLocation.Text
            _sqlComm.Parameters.Add("Tags", Data.SqlDbType.VarChar)
            _sqlComm.Parameters("Tags").Value = txtLocalTags.Text
            _sqlComm.Parameters.Add("TimeStamp", Data.SqlDbType.DateTime)
            _sqlComm.Parameters("TimeStamp").Value = DateTime.Now

            _con.Open()
            _sqlComm.ExecuteNonQuery()
            _con.Close()
            Dim logic = New connections
            logic.emailsToSend(User.Identity.Name, getURL, reportedBy)
            'sendEmail()
            Dim thread As New Thread(New ThreadStart(AddressOf sendEmail))
            thread.Start()
            Response.Redirect(getRedirectionPath)
        Catch ex As Exception
            MsgBox(ex.Message)
            Response.Write(ex.Message)
        Finally
            _con.Close()
            _con.Dispose()
            _sqlComm.Dispose()
        End Try
    End Sub
    Public Sub sendEmail()
        Dim _con As New SqlConnection(ConfigurationManager.ConnectionStrings("CitizenJDBConnectionString").ConnectionString)
        Dim _sqlDataAdapter As New SqlDataAdapter("SELECT * FROM EmailSender", _con)
        Dim _table As New System.Data.DataTable

        Try
            _con.Open()
            _sqlDataAdapter.Fill(_table)
            _con.Close()
            For i As Integer = 0 To _table.Rows.Count

                Dim AppPath As String = Request.PhysicalApplicationPath
                Dim sr As New StreamReader(AppPath & "EmailTemplates/NewReport.txt")
                Dim message As New MailMessage()
                message.IsBodyHtml = True
                message.From = New MailAddress("admin@asdf.com")
                message.To.Add(New MailAddress(_table.Rows(i).Item(1)))
                'message.CC.Add(New MailAddress("monodeep12@gmail.com"))
                message.Subject = "New User registration !"
                message.Body = sr.ReadToEnd()
                sr.Close()
                message.Body = message.Body.Replace("<%ReporterName%>", _table.Rows(i).Item(3))
                message.Body = message.Body.Replace("<%ReportURL%>", _table.Rows(i).Item(2))
                Dim client As New SmtpClient()
                client.Host = "smtp.tricedeals.com"
                'smtp.gmail.com
                client.Port = 25
                client.UseDefaultCredentials = True
                client.Credentials = New System.Net.NetworkCredential("admin@asdf.com", "111111")
                'smtp.EnableSsl = True
                client.Send(message)
                i += 1
            Next

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

我的代码出了什么问题

Response.End方法结束页面执行,并将执行转移到应用程序事件管道中的Application_EndRequest事件。没有执行Response.End后面的代码行。

此问题出现在Response.RRedirect和Server.Transfer方法中,因为这两个方法都在内部调用Response.End。

要解决此问题,请使用以下方法之一:

对于Response.End,请调用HttpContext.Current.ApplicationInstance.CompleteRequest方法,而不是Response.End,以绕过代码执行到Application_EndRequest事件。

对于Response.RRedirect,使用重载Response.DRedirect(String url,bool endResponse),该重载为endResponse参数传递false,以抑制对Response.End的内部调用。例如:Response.RRedirect("nextpage.aspx",false);

如果使用此解决方法,将执行Response.RRedirect后面的代码。对于Server.Transfer,请改用Server.Execute方法。

点击此处阅读更多