c# - 错误 : Excepiton in System. Threading.ThreadAbortException:线程被中止

标签 c# asp.net multithreading

我在下载模板时收到以下错误消息。

我试过代替 Response.Flush();与 Response.End();.但是得到同样的错误。

Error: Excepiton in Download:System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()

任何避免上述异常的想法

代码

private void DownloadFile(string filePath, string downloadFileName)
{
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("content-disposition", "attachment; filename=" + downloadFileName);
    Response.TransmitFile(filePath);
    // Response.Flush();
    Response.End();
}

提前致谢..

最佳答案

正如这里的回答:- How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download

Replace this : HttpContext.Current.Response.End();

With this :

HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline**

chain of execution and directly execute the EndRequest event.

并在这里回答:- ASP.NET exception "Thread was being aborted" causes method to exit

This is a ThreadAbortException; it's a special exception that is automatically rethrown at the end of every catch block, unless you call Thread.ResetAbort().

ASP .Net methods like Response.End or Response.Redirect (unless you pass false) throw this exception to end processing of the current page; your someFunctionCall() is probably calling one of those methods.

ASP .Net itself handles this exception and calls ResetAbort to continue processing.

关于c# - 错误 : Excepiton in System. Threading.ThreadAbortException:线程被中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691232/

相关文章:

c# - 使用 C# 进行 URL 编码

c# - System.Data.Entity.ModelConfiguration 不存在

c# - 通过 OpenId Connect 身份验证传递查询字符串参数

c# - 流利的 NHibernate SQL Server 2012

java - 多线程快速排序比预期慢很多

java - 线程会给应用程序增加很多开销吗?

java - 为java threadPool中的每个线程设置超时

c# - ComboBox 选择完成 MVVM 后强制绑定(bind)更新

用于处理不返回的非 void 方法的 C# 选项

c# - 如何在 asp.net TreeView 中的树节点中存储数组数据