Environment.HasShutdownStarted Property
Updated: September 2009
Gets a value indicating whether the common language runtime is shutting down or the current application domain is unloading.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Booleantrue if the common language runtime is shutting down or the current AppDomain is unloading; otherwise, false.
The current application domain is the AppDomain that contains the object that is calling HasShutdownStarted.
When the CLR shuts down, the execution engine of the CLR starts the finalizer thread to finalize objects before they are reclaimed by the garbage collector. Next, it raises the ProcessExit event. At this point, the system no longer behaves normally.
The HasShutdownStarted property returns true only after the finalizer thread has been started. This property returns false if the finalizer thread has not been started.
Use the HasShutdownStarted property to determine if you should perform any necessary cleanup operations in your object's Finalize method if the CLR is shutting down. After finalization, an object is accessible but in an invalid state; therefore, it is unusable. Shutdown is important to determine in finalization code because it is difficult to perform finalizations with objects that are already finalized.
You can also determine whether the CLR is shutting down by monitoring for the ProcessExit event. However, you can do this only from the default application domain.
In your Finalize method, do not access other objects that are referenced by static fields and that have finalization methods, because these objects might already have been finalized. An exception to this rule is the Console class, which contains static fields that reference stream objects; you can always write to the system console, even during the shutdown.
The following code example displays whether the common language runtime is shutting down.
// Sample for the Environment.HasShutdownStarted property using System; class Sample { public static void Main() { Console.WriteLine(); Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted); } } /* This example produces the following results: HasShutdownStarted: False */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.