c# - How to prevent an exception in a background thread from terminating an application? -


i can hookup appdomain.currentdomain.unhandledexception log exceptions background threads, how prevent them terminating runtime?

first, should try not have exceptions thrown - , not handled - in background thread. if control way delegate run, encapsulate in try catch block , figure way pass exception information main thread (using endinvoke if explicitly called begininvoke, or updating shared state somewhere).

ignoring unhandled exception can dangerous. if have real un-handlable exception (outofmemoryexception comes mind), there's not can anyway , process doomed.

back .net 1.1, unhandled exception in backgroundthread thrown , main thread gladly plough on. , have nasty repercussions. in .net 2.0 behavior has changed.

now, unhandled exception thrown in thread not main thread terminate process. may notified of (by subscribing event on appdomain) process die nonetheless.

since can inconvenient (when don't know run in thread , not absolutely sure it's guarded, , main thread must resilient), there's workaround. it's intended legacy settings (meaning, it's suggested make sure don't have stray threads) can force former behavior way :

just add setting service/application/whatever configuration file :

<configuration>   <runtime>     <!-- following setting prevents host closing when unhandled exception thrown -->     <legacyunhandledexceptionpolicy enabled="1" />   </runtime> </configuration> 

it doesn't seem work asp.net, though.

for more information (and huge warning setting may not supported in upcoming versions of clr) see http://msdn.microsoft.com/en-us/library/ms228965.aspx


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -