Today was actually the first time (that I can remember) I have come across the following error message:
"Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack"
Unfortuntely, today also happened to be a very long day following a very long night (working until 5am last night).
So when this error appeared in a fresh page, based 90% on my previous code I was stumped, angry and frustrated. Thankfully for me the error was simple to rectify, however it is important to note the error has a purpose rather than it being a bug.
This error crops up when terminating the Response to a page either through Response.End, Response.Redirect or Server.Trasnfer
You may not even notice the error 95% of the time that it occurs.
I had the lethal combination of a try-catch block and a Response.Redirect. I think I'll explain the significance of the error first and then the simple work-around.
Basically, the Response object is a .NET wrapped set of code that has a certain life-path. This path is supposed to end with a Response.End (either explicit or implied) that passes execution to the Application_EndRequest method. When the correct path is not followed, a ThreadAbortException is thrown.
I moved my Response.Redirect out of a try-catch block and appended the optional "terminate execution" parameter of "false" as follows:
Response.Redirect("going-somewhere.aspx", false);
This alleviated my problem and I was able to go back to swearing about something else that wasn't working as expected.
Microsoft's explanation and fixes are listed here: ThreadAbortException when using Response.Redirect