问题
I am currently working in .net c# 4.0 and have come across an issue with some code that I written that is causing me some headaches.
I am using the System.Threading.Tasks.TaskFactory class in conjunction with System.Threading.Tasks.TaskScheduler to start a new thread in my console application where the function of the thread is to check if an item has been added to a queue. When an item is added to the queue, it processes it.
So the queue contains emails to be sent and once an email is added to the queue, the email is sent via multiple clients. The sending to each is done in parallel.
I have this intermittent issue where the following exception occurs in the new thread some of the time:
[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
When debugging I am not able to get any further information as all properties in the stack have "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."
Looking on the internet for a solution, I have found this is an issue that occurs when doing a response.redirect but I am not doing this in my code. If I try to debug, there is no consistency as to where this error occurs. Following is the code I use to create the factory and start the new thread:
this.taskFactory = new TaskFactory(TaskScheduler.Current);
this.taskFactory.StartNew(this.DequeueMessage, state, TaskCreationOptions.LongRunning);
Has anyone any pointers as to why I may be getting this error and any tips as to how it might be fixed?
回答1:
The only reason that you get a ThreadAbortException
is because Thread.Abort
was called on the thread. If you're not doing that manually, this can happen in a client application when it terminates. It will also happen in hosted environments when things get recycled. If you provide more detail, a more specific answer could be provided.
来源:https://stackoverflow.com/questions/11527960/system-threading-threadabortexception-fired-in-new-thread