C# 由于代码已优化或本机框架位于调用堆栈顶部,因此无法计算表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15418012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
提问by Risho
I have two buttons with onclick
event handlers that process information from two grid views
on the page. One button for each gv. These are situated on top of each other nested in a html
table structure.
我有两个带有onclick
事件处理程序的按钮,可以处理grid views
页面上两个的信息。每个 gv 一个按钮。它们位于嵌套在html
表结构中的彼此之上。
The buttons are used to export the grid
date into an Excel
document (see code below)
这些按钮用于将grid
日期导出到Excel
文档中(请参阅下面的代码)
The top button and grid
when click
on the button works fine but the bottom button throws a ThreadAbortException: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
顶部按钮,grid
当click
按钮工作正常,但底部的按钮将引发ThreadAbortException: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
Naturally I've Google-d for this but some of the top results were dealing with Response.Redirect()
calls rather than Response.End()
. One such post at Forums.asp.nethas the same error on the same method call but the solution was to changing the code Response.Redirect()
with an error page as parameter - again not related to what I have.
自然,我为此使用了 Google-d,但一些最重要的结果是处理Response.Redirect()
调用而不是Response.End()
. Forums.asp.net上的一篇这样的帖子在同一方法调用上有相同的错误,但解决方案是Response.Redirect()
使用错误页面作为参数更改代码- 再次与我所拥有的无关。
Another search at Microsoft Suport pagesuggest a solution where HttpContext.Current.ApplicationInstance.CompleteRequest()
replaces Response.End()
. I've tried this, the error goes away and so is the Excel download popup.
Microsoft 支持页面上的另一个搜索建议了一个解决方案,其中HttpContext.Current.ApplicationInstance.CompleteRequest()
替换Response.End()
. 我试过了,错误消失了,Excel 下载弹出窗口也是如此。
So I don't know where to go from here. What's weird is that the same code (less gridview
id) works for one but the other. Here is the code for your review and I've marked where the error is thrown.
I thought perhaps I could spawn an new thread - would that alleviate the issue? I've never done a multi-threaded
app but I'm up for a challenge.
所以我不知道从这里去哪里。奇怪的是,相同的代码(更少的gridview
id)适用于另一个。这是供您查看的代码,我已经标记了错误发生的位置。我想也许我可以创建一个新线程 - 这会缓解这个问题吗?我从未做过multi-threaded
应用程序,但我准备迎接挑战。
<table>
<tr>
<td align="left">
<asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click"
Text="Export" Visible="false" />
</td>
</tr>
<tr>
<td>
<asp:Panel runat="server" ID="pnl1" Visible="false">
<asp:GridView ID="gvCountTotalsCat" runat="server"
AutoGenerateColumns="false"
CellPadding="3" PageSize="25" BackColor="White" BorderColor="MidnightBlue"
BorderStyle="Groove" BorderWidth="1px" CssClass="TextCompact"
GridLines="Vertical"
OnRowDataBound="gridView_OnRowDataBound"
EmptyDataText="Your request has returned zero records">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeader" Text="Cat" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litWuc" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Cat Entries" HeaderText="Cat Entries" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeader" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litSum" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
<tr>
<td align="left">
<asp:Button ID="btnExport1" runat="server" OnClick="btnExport_Click1"
Text="Export" Visible="false" />
</td>
</tr>
<tr>
<td>
<asp:Panel runat="server" ID="pnl2" Visible="false">
<asp:GridView ID="gvCountTotalsCat1" runat="server"
AutoGenerateColumns="false"
AllowPaging="false" CellPadding="3" PageSize="25" BackColor="White"
BorderColor="MidnightBlue" BorderStyle="Groove" BorderWidth="1px"
CssClass="TextCompact" GridLines="Vertical"
OnRowDataBound="gridView_OnRowDataBound"
EmptyDataText="Your request has returned zero records">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeaderWuc" Text="Wuc" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litWuc" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Wuc Entries" HeaderText="Wuc Entries" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label runat="server" ID="lblHeader" />
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="litSum" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
</table>
public void btnExport_Click(object sender, System.EventArgs e)
{
string attachment = string.Empty;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";
attachment = "attachment; filename=gvCountTotalsCat_" + _selectedSite + ".xls";
gvCountTotalsCat.Parent.Controls.Add(frm);
frm.Controls.Add(gvCountTotalsCat);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public void btnExport_Click1(object sender, System.EventArgs e)
{
string attachment = string.Empty;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.Clear();
Response.ClearHeaders();
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";
attachment = "attachment; filename=gvCountTotalsCat1_" + _selectedSite + ".xls";
gvCountTotalsCat1.Parent.Controls.Add(frm);
frm.Controls.Add(gvCountTotalsCat1);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
frm.RenderControl(htw);
Response.Write(sw.ToString());
try
{
>> Error thrown here >> Response.End();
}
catch (System.Threading.ThreadAbortException lException)
{
lException;
}
}
采纳答案by Risho
It runed out that I have both grids and buttons in one Update Panel
and only the top button was set as a PostBackTrigger
. After I added the second one <asp:PostBackTrigger ControlID="btnExport1" />
and that solve the problem.
结果发现我同时拥有网格和按钮,Update Panel
并且只有顶部按钮被设置为PostBackTrigger
. 在我添加了第二个之后<asp:PostBackTrigger ControlID="btnExport1" />
,问题就解决了。
回答by Santhosh
There is a handy way to deal with "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." issues. You need to write on the Output window.
有一种方便的方法可以处理“无法计算表达式,因为代码已优化或本机框架位于调用堆栈顶部。” 问题。您需要在输出窗口上书写。
Add using System.Diagnostics;
添加使用 System.Diagnostics;
Add a Try/Catch for the line that is erroring
为出错的行添加 Try/Catch
In the Catch add these lines
在 Catch 添加这些行
try
{ ..}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
Debug.WriteLine(ex.InnerException.ToString());
}
Just debug and check the Output window
只需调试并检查输出窗口
Hope it helps.
希望能帮助到你。
回答by ChocapicSz
Can be solved using an ASPxGridViewExporter
without getting a ThreadAbortException
.
可以使用 an 解决ASPxGridViewExporter
而无需获取ThreadAbortException
.
Try the following code snippet:
尝试以下代码片段:
try
{
using (MemoryStream stream = new MemoryStream())
{
// Write the content of the xlsx to the stream
gridViewExporter.WriteXlsx(stream, new XlsxExportOptions(TextExportMode.Text, false, false));
if (Response == null)
{
return;
}
// Write the byte content to the output
Response.Clear();
Response.AppendHeader("Content-Disposition", StringMngr.SafeFormat("attachment; filename=\"{0}.xlsx\"", "xlsxFileName"));
Response.ContentType = "Text/xlsx";
Response.ContentEncoding = System.Text.Encoding.Unicode;
if (stream.Length > 0)
{
Response.BinaryWrite(stream.ToArray());
}
Response.Flush();
Response.SuppressContent = true;
}
}
catch (Exception ex)
{
log.Error("An error occured while downloading in xlsx format.", ex);
}
It downloads the excel file without getting a ThreadAbortException
with the message of:
它下载 excel 文件而没有收到ThreadAbortException
以下消息:
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
由于代码已优化或本机帧位于调用堆栈顶部,因此无法计算表达式。
and without getting the following error message when opening the excel file:
并且在打开 excel 文件时没有收到以下错误消息:
Excel found unreadble content in myFilename.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this book, click Yes.
Excel 在 myFilename.xlsx 中发现无法读取的内容。是否要恢复此工作簿的内容?如果您信任本书的来源,请单击是。
Hope it helps! :)
希望能帮助到你!:)