如何在C#.NET中的线程之间锁定控制台? [英] How do I lock the console across threads in C#.NET?

查看:220
本文介绍了如何在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.

这篇关于如何在C#.NET中的线程之间锁定控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆