예제 #1
0
        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        /// <summary>
        /// Makes sure a console instance is attached to the process.
        /// </summary>
        /// <returns>
        /// A disposable console object.
        /// </returns>
        /// <remarks>
        /// <para>
        /// If a console is already attached (see <see cref="HasConsole"/>), this method and the
        /// returned <see cref="IDisposable"/> do nothing. If no console is attached, a new
        /// console will be attached. The temporary console will be detached when the returned
        /// <see cref="IDisposable"/> is disposed. When the console is being closed, the user must
        /// press a key ("Press any key to continue...") and the temporary console and the
        /// application is blocked until the key is pressed.
        /// </para>
        /// <para>
        /// This method is often used in a <see langword="using"/> statement like this:
        /// </para>
        /// <code lang="csharp" title="Example: Using AttachConsole in a using statement.">
        /// <![CDATA[
        /// using (ConsoleHelper.AttachConsole)
        /// {
        ///   Console.WriteLine("Important message...");
        /// }
        /// ]]>
        /// </code>
        /// <para>
        /// If a temporary console has to be attached, it is best to keep this console alive as long
        /// as you need it. Do not call <see cref="AttachConsole"/>/<see cref="DetachConsole"/>
        /// several time in the application lifetime.
        /// </para>
        /// </remarks>
        public static IDisposable AttachConsole()
        {
            var hasConsole    = HasConsole;
            var consoleRegion = new ConsoleRegion(hasConsole);

            if (!hasConsole)
            {
                NativeMethods.AllocConsole();

                // Initialize stdout and stderror. Code taken from
                // http://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application.
                Type       type  = typeof(Console);
                FieldInfo  @out  = type.GetField("_out", BindingFlags.Static | BindingFlags.NonPublic);
                FieldInfo  error = type.GetField("_error", BindingFlags.Static | BindingFlags.NonPublic);
                MethodInfo initializeStdOutError = type.GetMethod("InitializeStdOutError", BindingFlags.Static | BindingFlags.NonPublic);
                Debug.Assert(@out != null);
                Debug.Assert(error != null);
                Debug.Assert(initializeStdOutError != null);
                @out.SetValue(null, null);
                error.SetValue(null, null);
                initializeStdOutError.Invoke(null, new object[] { true });
            }

            return(consoleRegion);
        }
예제 #2
0
        //--------------------------------------------------------------
        /// <summary>
        /// Makes sure a console instance is attached to the process.
        /// </summary>
        /// <returns>
        /// A disposable console object.
        /// </returns>
        /// <remarks>
        /// <para>
        /// If a console is already attached (see <see cref="HasConsole"/>), this method and the 
        /// returned <see cref="IDisposable"/> do nothing. If no console is attached, a new 
        /// console will be attached. The temporary console will be detached when the returned
        /// <see cref="IDisposable"/> is disposed. When the console is being closed, the user must 
        /// press a key ("Press any key to continue...") and the temporary console and the 
        /// application is blocked until the key is pressed.
        /// </para>
        /// <para>
        /// This method is often used in a <see langword="using"/> statement like this:
        /// </para>
        /// <code lang="csharp" title="Example: Using AttachConsole in a using statement.">
        /// <![CDATA[
        /// using (ConsoleHelper.AttachConsole)
        /// {
        ///   Console.WriteLine("Important message...");
        /// }
        /// ]]>
        /// </code>
        /// <para>
        /// If a temporary console has to be attached, it is best to keep this console alive as long
        /// as you need it. Do not call <see cref="AttachConsole"/>/<see cref="DetachConsole"/>
        /// several time in the application lifetime.
        /// </para>
        /// </remarks>
        public static IDisposable AttachConsole()
        {
            var hasConsole = HasConsole;
            var consoleRegion = new ConsoleRegion(hasConsole);

            if (!hasConsole)
            {
                NativeMethods.AllocConsole();

                // Initialize stdout and stderror. Code taken from
                // http://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application.
                Type type = typeof(Console);
                FieldInfo @out = type.GetField("_out", BindingFlags.Static | BindingFlags.NonPublic);
                FieldInfo error = type.GetField("_error", BindingFlags.Static | BindingFlags.NonPublic);
                MethodInfo initializeStdOutError = type.GetMethod("InitializeStdOutError", BindingFlags.Static | BindingFlags.NonPublic);
                Debug.Assert(@out != null);
                Debug.Assert(error != null);
                Debug.Assert(initializeStdOutError != null);
                @out.SetValue(null, null);
                error.SetValue(null, null);
                initializeStdOutError.Invoke(null, new object[] { true });
            }

            return consoleRegion;
        }