.NET Framework Setup
System.Threading.ThreadAbortException
Question:
I Keep getting warnings in the event log of System.Threading.ThreadAbortException. This only happens on a production server and not on a development server.
Prod Server
win2k, sp4
RAM - max2GB, installed 4GB with 4 slots
CPU - P3 99MHZ
-----
Error:
Page: details.aspx?AID=4987
Message: Thread was being aborted.
Source: mscorlib
Method: Void AbortInternal()
Stack Trace:
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()
at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
at System.Web.HttpResponse.Redirect(String url)
at details.UpdateData(String id)
-----
Code:
protected void btnProcess_Click(object sender, EventArgs e)
{
//update sql
Label CCPIdLabel = ((Label)(FormView1.Row.FindControl("CCPIdLabel")));
string strId = CCPIdLabel.Text;
UpdateData(strId);
}
private void UpdateData(string id)
{
try
{
//convert string id to int
int iID;
if (null == id || 0 == id.Length) //validate data
{
return;
}
else
{
iID = Int32.Parse(id);
}
//sql update
SqlCommand cmd = new SqlCommand("uspUpdateCCPayment", new SqlConnection(myConn));
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@CCPaymentId", System.Data.SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@ProcessedBy", System.Data.SqlDbType.VarChar, 100));
cmd.Parameters["@CCPaymentId"].Value = iID;
cmd.Parameters["@ProcessedBy"].Value = myDomainUser.ToString(); //accountName;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
//redirect page to queue
Response.Redirect("default.aspx");
}
catch (Exception ex)
{
//log error
Utilities.LogError(ex);
Response.Redirect("error.aspx");
}
}
-----
Any suggestions?
Answer1:
You are not using the second Using statement in C# which calls dispose for you automatically on classes that implements IDisposable and you just closed the connection without call dispose.
An Elliptic curve is modular ~ Taniyama and Shimora
Answer2:
not sure I follow.
here is what I'm referencing:
using
System;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
using
System.Drawing;
using
System.Globalization;
using
System.Net.Mail;
using
System.Security.Principal;
using
System.Security.Cryptography;
using
System.Text.RegularExpressions;
using
System.Threading;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
Answer3:
I was not talking about those using statement I was talking about the second one it calls Dispose for you automatically on classes that implement IDisposable interface. And the main cause of your thread abort is the Response.Redirect if you are not making the round trip you can try using Server.Transfer. Look at the ExecuteNonQeury code in the first link for the second Using statement and the second one covers Response.Redirect and Server.Transfer.
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx
http://weblogs.asp.net/jgalloway/archive/2004/07/21/189547.aspx
An Elliptic curve is modular ~ Taniyama and Shimora
Answer4:
I've done this plenty of times on a Windows 2003 server and never encountered this issue. So should I use the following?
Response.Redirect("default.aspx", false);
Answer5:
EDIT
In the code you posted you have a connection that needs to be disposed and if you are sending the users to another page of the same application it is better to use Server.Transfer. We tested it if your users are sent to a different page in the same application you don't need Response.Redirect. Try to read the comments in the blog link I posted.
An Elliptic curve is modular ~ Taniyama and Shimora
Answer6:
Response.Redirect calls Response.End, which calls code that ends up throwing a ThreadAbortException. See this article on why that is a good thing, and how to work around the issue.
http://www.sagara.net/archive/2008/04/05/response.redirect-and-the-threadabortexception-and-why-its-a-good-thing.aspx