System.Threading.ThreadAbortException: 正在中止线程

来源:互联网 发布:全球数据库市场份额 编辑:程序博客网 时间:2024/05/21 21:36

System.Threading.ThreadAbortException: 正在中止线程

最近做的系统中老出现的一些问题不太明白,在使用 Response.End、Response.Redirect 或 Server.Transfer 时出现 ThreadAbortException ,本来系统是没有问题的,在保存数据时也可以正常,本来使用try-catch 语句是用来捕获一异常情况的,但系统正常,老捕获到下面的东西

##[操作记录]:2007-11-23 9:25:12  System.Threading.ThreadAbortException: 正在中止线程。
   在 System.Threading.Thread.AbortInternal()
   在 System.Threading.Thread.Abort(Object stateInfo)
   在 System.Web.HttpResponse.End()
   在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
   在 System.Web.HttpResponse.Redirect(String url)
   在 rsdl.ind_arcAddup.BtnSaveExit_Click(Object sender, EventArgs e) 位置 D:\rsdl\app\arc\ind_arcAddup.aspx.vb:行号 525

代码如下:
System.Threading.ThreadAbortException: <wbr>正在中止线程Protected Sub BtnSaveExit_Click(ByVal sender As ObjectByVal As System.EventArgs) Handles BtnSaveExit.Click
System.Threading.ThreadAbortException: <wbr>正在中止线程 '保存退出
System.Threading.ThreadAbortException: <wbr>正在中止线程
 If LtlarcID.Text <> "" Then
System.Threading.ThreadAbortException: <wbr>正在中止线程
System.Threading.ThreadAbortException: <wbr>正在中止线程 If reconredTextBox.Text <> LtlReconred.Text Then
System.Threading.ThreadAbortException: <wbr>正在中止线程 Pf.ShowMessage(Me"您修改了档案号!无法保存!如果你必须修改档案号,请与管理员联系!")
System.Threading.ThreadAbortException: <wbr>正在中止线程 Exit Sub
System.Threading.ThreadAbortException: <wbr>正在中止线程 End If
System.Threading.ThreadAbortException: <wbr>正在中止线程
System.Threading.ThreadAbortException: <wbr>正在中止线程 End If
System.Threading.ThreadAbortException: <wbr>正在中止线程 vDate()
System.Threading.ThreadAbortException: <wbr>正在中止线程 If IIf(LtlarcID.Text "", 0, LtlarcID.Text) Then
System.Threading.ThreadAbortException: <wbr>正在中止线程 HisArcActLog("增加用户档案信息", "档案表", "档案号" reconredTextBox.Text "," nameTextBox.Text, 0)
System.Threading.ThreadAbortException: <wbr>正在中止线程 Else
System.Threading.ThreadAbortException: <wbr>正在中止线程 HisArcActLog("修改用户档案信息", "档案表", "档案号" reconredTextBox.Text "," nameTextBox.Text, LtlarcID.Text)
System.Threading.ThreadAbortException: <wbr>正在中止线程 End If
System.Threading.ThreadAbortException: <wbr>正在中止线程
System.Threading.ThreadAbortException: <wbr>正在中止线程 Try
System.Threading.ThreadAbortException: <wbr>正在中止线程 SaveData()
System.Threading.ThreadAbortException: <wbr>正在中止线程 Response.Redirect("ind_arcManage.aspx") '此行号为 525 
System.Threading.ThreadAbortException: <wbr>正在中止线程
 Catch ex As Exception
System.Threading.ThreadAbortException: <wbr>正在中止线程 LogError(ex.ToString, "~/ErrLog.txt")
System.Threading.ThreadAbortException: <wbr>正在中止线程 ShowMessage(Me"该档案号已经在使用,或信息填写错误,无法保存!\n\n如果该档案号是录入错误而删除,请与管理员联系清空该档案号!")
System.Threading.ThreadAbortException: <wbr>正在中止线程 End Try
System.Threading.ThreadAbortException: <wbr>正在中止线程 
System.Threading.ThreadAbortException: <wbr>正在中止线程 End Sub
 看看从网上找来的一些资料才明白是什么原因,按以下方法排除了此问题。
 

症状

如果使用 Response.EndResponse.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常。您可以使用 try-catch 语句捕获此异常。
 

原因

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 方法。