A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/dotnet/framework/performance/constrained-execution-regions below:

Constrained Execution Regions - .NET Framework

A constrained execution region (CER) is part of a mechanism for authoring reliable managed code. A CER defines an area in which the common language runtime (CLR) is constrained from throwing out-of-band exceptions that would prevent the code in the area from executing in its entirety. Within that region, user code is constrained from executing code that would result in the throwing of out-of-band exceptions. The PrepareConstrainedRegions method must immediately precede a try block and marks catch, finally, and fault blocks as constrained execution regions. Once marked as a constrained region, code must only call other code with strong reliability contracts, and code should not allocate or make virtual calls to unprepared or unreliable methods unless the code is prepared to handle failures. The CLR delays thread aborts for code that is executing in a CER.

Important

CER is only supported in .NET Framework. This article doesn't apply to .NET Core or .NET 5 and above.

Constrained execution regions are used in different forms in the CLR in addition to an annotated try block, notably critical finalizers executing in classes derived from the CriticalFinalizerObject class and code executed using the ExecuteCodeWithGuaranteedCleanup method.

CER Advance Preparation

The CLR prepares CERs in advance to avoid out-of-memory conditions. Advance preparation is required so the CLR does not cause an out of memory condition during just-in-time compilation or type loading.

The developer is required to indicate that a code region is a CER:

Constraints

Users are constrained in the type of code they can write in a CER. The code cannot cause an out-of-band exception, such as might result from the following operations:

In .NET Framework version 2.0, these constraints are guidelines. Diagnostics are provided through code analysis tools.

Reliability Contracts

The ReliabilityContractAttribute is a custom attribute that documents the reliability guarantees and the corruption state of a given method.

Reliability Guarantees

Reliability guarantees, represented by Cer enumeration values, indicate the degree of reliability of a given method:

Corruption levels

Corruption levels, represented by Consistency enumeration values, indicate how much state may be corrupted in a given environment:

Reliability try/catch/finally

The reliability try/catch/finally is an exception handling mechanism with the same level of predictability guarantees as the unmanaged version. The catch/finally block is the CER. Methods in the block require advance preparation and must be noninterruptible.

In .NET Framework version 2.0, code informs the runtime that a try is reliable by calling PrepareConstrainedRegions immediately preceding a try block. PrepareConstrainedRegions is a member of RuntimeHelpers, a compiler support class. Call PrepareConstrainedRegions directly pending its availability through compilers.

Noninterruptible Regions

A noninterruptible region groups a set of instructions into a CER.

In .NET Framework version 2.0, pending availability through compiler support, user code creates non-interruptible regions with a reliable try/catch/finally that contains an empty try/catch block preceded by a PrepareConstrainedRegions method call.

Critical Finalizer Object

A CriticalFinalizerObject guarantees that garbage collection will execute the finalizer. Upon allocation, the finalizer and its call graph are prepared in advance. The finalizer method executes in a CER, and must obey all the constraints on CERs and finalizers.

Any types inheriting from SafeHandle and CriticalHandle are guaranteed to have their finalizer execute within a CER. Implement ReleaseHandle in SafeHandle derived classes to execute any code that is required to free the handle.

Code Not Permitted in CERs

The following operations are not permitted in CERs:

See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4