0

I am getting error like

System.Threading.ThreadAbortException: Thread was being aborted.

  at System.String.wstrcpy(Char* dmem, Char* smem, Int32 charCount)

  at System.String.FillStringChecked(String dest, Int32 destPos, String src)

  at System.String.Concat(String str0, String str1)

  at reporterror.Page_Load(Object sender, EventArgs e)

Can anyone help to know why this error comes? Why Thread was being aborted error comes?

IT researcher
  • 3,274
  • 17
  • 79
  • 143
  • 1
    Are you using `Response.Redirect()`? If so, read [this SO post](http://stackoverflow.com/questions/13727422/can-endresponse-increase-performance-of-asp-net-page/13727769#13727769). – MikeSmithDev Aug 22 '14 at 11:31
  • @MikeSmithDev I am not using Response.Redirect . I only concat strings.But they are very large in length. – IT researcher Aug 22 '14 at 12:45

2 Answers2

1

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.

In other words wstrcpy called Thread.Abort()

EDIT: Or main thread called Abort() on Thread that Page_Load() was running on.

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • RE "`wstrcpy` called `Thread.Abort()`": not strictly true, the Thread could have been aborted from any piece of code and this exception would have been thrown. – Sean Airey Aug 22 '14 at 10:52
  • @Sean How come then stack trace reported starts at `wstrcpy`? – Matas Vaitkevicius Aug 22 '14 at 10:54
  • If you call a `Thread.Abort()` on any thread (or it is called from somewhere else, in ASP.Net this is commonly caused by a `Response.Redirect`), the exception will be thrown from within the thread being aborted, not on the thread that aborted it. – Sean Airey Aug 22 '14 at 10:56
  • @Sean are you implying then that `System.String.wstrcpy` spawned some other thread that got aborted? I am not sure what you are getting at? Are you saying that it could have been aborted in let's say `Page_Load` and it spat out trace from `wstrcpy` ? – Matas Vaitkevicius Aug 22 '14 at 10:59
  • 1
    No, I'm saying that the thread that was executing `wstrcopy` at that time was aborted by an outside thread. – Sean Airey Aug 22 '14 at 11:07
  • [Quick demo](http://pastebin.com/d34CvFdi), if you duplicate this in a fresh console application and then turn on the setting to warn on thrown CLR exceptions from the Debug menu, you'll get the `ThreadAbortException` in `DoWork` and the main thread will continue. – Sean Airey Aug 22 '14 at 11:12
  • Alternatively, [this program](http://pastebin.com/pxFJs8u8) will output "I aborted the thread." and "DoWork's thread was aborted.". – Sean Airey Aug 22 '14 at 11:16
  • @Sean Yes, I see what you mean, it's still surprising that in the first example I am not getting exception thrown in debug mode. – Matas Vaitkevicius Aug 22 '14 at 11:24
  • @Sean Another surprising thing is that messages come in "I aborted the thread." and then "DoWork's thread was aborted." is it because exception is thrown on `t` thread and not on Main, that would explain your version. – Matas Vaitkevicius Aug 22 '14 at 11:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59808/discussion-between-sean-and-liufa). – Sean Airey Aug 22 '14 at 12:02
1

Not sure if this is why you are seeing this, but in ASP.NET if you redirect out of a catch block you will get Thread was being aborted

catch (Exception ex)
{
   //This will throw  "Thread was being aborted"
   response.redirect("~\default.aspx");
}
Elim Garak
  • 1,728
  • 1
  • 16
  • 21