我的代码出了什么问题?我在按钮单击事件中启动线程。我没有收到任何错误,但它没有触发send email sub。
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 WORT BEORT A ATAC”
更新:根据要求,我上传了整个代码段
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
答案 0 :(得分:3)
Response.End方法结束页面执行并将执行转移到应用程序事件管道中的Application_EndRequest事件。不执行Response.End之后的代码行。
Response.Redirect和Server.Transfer方法中出现此问题,因为两个方法都在内部调用Response.End。
要解决此问题,请使用以下方法之一:
对于Response.End,调用HttpContext.Current.ApplicationInstance.CompleteRequest方法而不是Response.End来绕过代码执行到Application_EndRequest事件。
对于Response.Redirect,使用一个重载,Response.Redirect(String url,bool endResponse),它为endResponse参数传递false以禁止对Response.End的内部调用。例如: Response.Redirect(“nextpage.aspx”,false);
如果使用此解决方法,则会执行Response.Redirect之后的代码。 对于Server.Transfer,请改用Server.Execute方法。
阅读更多Here