MSDN Magazine - December 2007 - (Page 22) accesses to resources without using some of the techniques we describe, and your code is used in a host that recycles AppDomains, your library can eventually cause that host to fail. Constrained Execution Regions A constrained execution region,or CER,is a reliability primitive that helps enable code to maintain consistency.This is best for when you are willing to work extremely hard to guarantee some limited amount of forward progress or ability to undo changes to an object.Constrained execution regions are a best-effort attempt from the runtime to run your code. With CERs, the runtime will hoist any CLR-induced failures to predictable locations. This doesn’t guarantee that your code will run—you can’t sprinkle CERs like fairy dust and expect your code to magically work—but if you’re writing code with some rigid constraints, there is a good chance your code will run to completion. CERs are exposed in three forms: • With ExecuteCodeWithGuaranteedCleanup,which is a stackoverflow safe form of a try/finally. • As a try/finally block preceded immediately by a call to RuntimeHelpers.PrepareConstrainedRegions. In this case, the try block isn’t constrained, but all catch, finally, and fault blocks for that try are constrained. • As a critical finalizer.Here,any subclass of CriticalFinalizerObject has a finalizer that is eagerly prepared before an instance of the object is allocated. (Note that there is a special case: SafeHandle’s ReleaseHandle method,a virtual method that is eagerly prepared before the subclass is allocated and called from SafeHandle’s critical finalizer.) Clean Up Code You should use try/finally blocks and finalizers to clean up resources.As a first approximation,these tools give developers a simple way to restore state to a reasonable level of consistency.Try/finally blocks (and language-specific keywords like the C# A constrained execution “using”statement) will imregion, or CER, is a reliability prove correctness and perprimitive that helps formance by ensuring that resources are cleaned up or enable code to maintain disposed of at deterministic consistency. points in your code. The problem with this approach,though,is that the CLR cannot easily guarantee that the code in your finally block will complete.While the CLR attempts to avoid aborting threads while running in finally blocks,resource exhaustion and thus async exceptions can occur at any time. Likewise,we have no way of ensuring that any given finalizer will complete.Additionally,the CLR’s escalation policy allows a host to abort threads in finally blocks and finalizers in case a finally block goes into an infinite loop or blocks indefinitely. Advanced Document Imaging Toolkits The DotImage 5.0 release includes enhancements to Add-On modules like Advanced Document Clean-Up (ADC) to help improve compression and archiving as well as increase readability and OCR accuracy. Border/Speck/Hole Punch/ Line/Halftone Removal, Border/Margin Crop, Blank Page Detection, and Automatic Page/Text Inversion are just a few of these new features in DotImage 5.0. Visit Atalasoft.com for a free trial download. Atalasoft, Your .NET Imaging Partner Call us toll free at 866-568-0129 www.atalasoft.com 22 msdnmagazine CLR Inside Out http://www.atalasoft.com http://Atalasoft.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.