[英]whats wrong with my code?
What's wrong with my code?? 我的代码有什么问题?? I am initiating the thread in a button click event.
我正在按钮单击事件中启动线程。 I do not get any error but it does not fire the 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
Why does not it work? 为什么不起作用?
Update: I get the error "THREAD WAS BEING ABORTED" 更新:我收到错误消息“线程被终止”
Update: As requested I have uploaded the whole code snippet 更新:根据要求,我已经上传了完整的代码段
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
The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. Response.End方法结束页面执行,并将执行转移到应用程序的事件管道中的Application_EndRequest事件。 The line of code that follows Response.End is not executed.
Response.End之后的代码行未执行。
This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally. 由于两个方法都在内部调用Response.End,因此在Response.Redirect和Server.Transfer方法中会发生此问题。
To work around this problem, use one of the following methods: 要变通解决此问题,请使用下列方法之一:
For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event. 对于Response.End,请调用HttpContext.Current.ApplicationInstance.CompleteRequest方法(而不是Response.End),以将代码执行绕过Application_EndRequest事件。
For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End. 对于Response.Redirect,请使用重载Response.Redirect(String url,bool endResponse),为endResponse参数传递false,以禁止对Response.End的内部调用。 For example: Response.Redirect ("nextpage.aspx", false);
例如:Response.Redirect(“ nextpage.aspx”,false);
If you use this workaround, the code that follows Response.Redirect is executed. 如果您使用此替代方法,则会执行Response.Redirect之后的代码。 For Server.Transfer, use the Server.Execute method instead.
对于Server.Transfer,请改用Server.Execute方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.