System.ConsolePal.EnsureInitializedCore C# (CSharp) Method

EnsureInitializedCore() private static method

Ensures that the console has been initialized for use.
private static EnsureInitializedCore ( ) : void
return void
        private static void EnsureInitializedCore()
        {
            lock (Console.Out) // ensure that writing the ANSI string and setting initialized to true are done atomically
            {
                if (!s_initialized)
                {
                    // Ensure the console is configured appropriately.  This will start
                    // signal handlers, etc.
                    if (!Interop.Sys.InitializeConsole())
                    {
                        throw Interop.GetExceptionForIoErrno(Interop.Sys.GetLastErrorInfo());
                    }

                    // Provide the native lib with the correct code from the terminfo to transition us into
                    // "application mode".  This will both transition it immediately, as well as allow
                    // the native lib later to handle signals that require re-entering the mode.
                    if (!Console.IsOutputRedirected)
                    {
                        string keypadXmit = TerminalFormatStrings.Instance.KeypadXmit;
                        if (keypadXmit != null)
                        {
                            Interop.Sys.SetKeypadXmit(keypadXmit);
                        }
                    }

                    // Load special control character codes used for input processing
                    var controlCharacterNames = new Interop.Sys.ControlCharacterNames[4] 
                    {
                        Interop.Sys.ControlCharacterNames.VERASE,
                        Interop.Sys.ControlCharacterNames.VEOL,
                        Interop.Sys.ControlCharacterNames.VEOL2,
                        Interop.Sys.ControlCharacterNames.VEOF
                    };
                    var controlCharacterValues = new byte[controlCharacterNames.Length];
                    Interop.Sys.GetControlCharacters(controlCharacterNames, controlCharacterValues, controlCharacterNames.Length, out s_posixDisableValue);
                    s_veraseCharacter = controlCharacterValues[0];
                    s_veolCharacter = controlCharacterValues[1];
                    s_veol2Character = controlCharacterValues[2];
                    s_veofCharacter = controlCharacterValues[3];

                    // Mark us as initialized
                    s_initialized = true;
                }
            }
        }