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. The line of code that follows Response.End is not executed.
This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally.
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.
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. For example: Response.Redirect ("nextpage.aspx", false);
If you use this workaround, the code that follows Response.Redirect is executed. For Server.Transfer, use the Server.Execute method instead.
Read More Here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.