Open In App

C# Program to Check Whether the CLR is Shutting Down or Not Using Environment Class

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the call stack information, and time since the last system boot in milliseconds information. By just using some predefined methods we can get the information of the Operating System using the Environment class. Here in this article, we will check whether the CLR(common language runtime) is shutting down or not. So we use the HasShutdownStarted property of the Environment class. This property is used to check whether the CLR is shutting down or the application domain is being unloaded or not. If the CLR is shut down then it will return true. Otherwise, it will return false if the is not CLR is shut down.

In the dot net framework, when an application domain is unloaded by CLR, then it executes finalizers on all the objects that contain finalizer function in that application domain. So when the CLR is shut down, then it will execute finalizers threads on all the objects that have the finalizer function. Hence this property returns true when the finalizer thread is started otherwise returns false.

Syntax:

bool Environment.HasShutdownStarted

Return Type: the return type of this property is boolean. Returns true if the CLR is shutting otherwise false.

Example:

C#




// C# program to check whether the CLR is shutdown or not
// Using Environment class
using System;
  
class GFG
{
  
    static public void Main()
    {
  
        // Declare a variable
        bool result;
  
        // Determining whether the CLR is shutting down or not
        // Using HasShutdownStarted property of Environment class
        result = Environment.HasShutdownStarted;
  
        // Displaying result
        if (result == true)
            Console.WriteLine("Yes! CLR is shutting down");
        else
            Console.WriteLine("No! CLR is not shutting down");
    }
}


Output:

No! CLR is not shutting down

Last Updated : 01 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads