如何在C#.NET中的线程之间锁定控制台?

问题描述:

我有一个 logger 课程处理各种颜色漂亮的信息显示(yay。)。但是,由于它以分隔的步骤)将控制台中的颜色设置为红色,写入文本,将颜色设置为灰色,写入文本,以呈现[Error] Description。 ..错误在红色),但我有一个多线程的应用程序,所以步骤可以混合起来和随机颜色打印随机的东西。

I have a logger class that handles various information display with pretty colors (yay.). However, since it writes to the console in separated steps (i.e. set color to red, write text, set color to gray, write text, for something that would render "[Error] Description..." with the error being in red), but I have a multithreaded application, so the steps can get mixed up and print random stuff in random colors.

我知道 lock 关键字,但它不能用于一个静态类,如控制台。

I am aware of the lock keyword, however it will not work with a static class such as the console.

这里是一些示例代码,如果我不清楚:

Here is some example code if I was unclear:

using System;
using System.Text;

    namespace N.Utilities.IO
    {
        public static class Logger
        {
            private static void WriteColored(string value, ConsoleColor color)
            {
                if (Logger.UseColor)
                {
                    Console.ForegroundColor = color;
                    Console.Write(value);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                else
                {
                    Console.Write(value);
                }
            }   

            private static void WriteLineColored(string value, ConsoleColor color)
            {
                if (Logger.UseColor)
                {
                    Console.ForegroundColor = color;
                    Console.WriteLine(value);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                else
                {
                    Console.Write(value);
                }
            }

            private static bool useColor = true;

            public static bool UseColor
            {
                get
                {
                    return Logger.useColor;
                }
                set
                {
                    Logger.useColor = value;
                }
            }

            public static void Inform(string value)
            {
                Logger.WriteColored("    [Info] ", ConsoleColor.White);
                Console.WriteLine(value);
            }

            public static void Warn(string value)
            {
                Logger.WriteColored(" [Warning] ", ConsoleColor.Yellow);
                Console.WriteLine(value);
            }

            public static void Error(string value)
            {
                Logger.WriteColored("   [Error] ", ConsoleColor.Red);
                Console.WriteLine(value);
            }
    }


class需要:

private static readonly object ConsoleWriterLock = new object();

然后,您可以在写入控制台之前锁定。

Then you can lock on this before writing to the console.

lock(ConsoleWriterLock)
{
     //Your code here
}

lock关键字将使用静态类,您只需要提供静态readonly 要锁定的对象。

The lock keyword will work with a static class, you just need to provide a static readonly object to lock on.