How do I lock the console across threads in C#.NET?
Asked Answered
A

1

14

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.

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);
            }
    }
Aleasealeatory answered 5/10, 2009 at 23:37 Comment(0)
C
24

Your class needs:

private static readonly object ConsoleWriterLock = new object();

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

lock(ConsoleWriterLock)
{
     //Your code here
}

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

Chippy answered 5/10, 2009 at 23:49 Comment(1)
@Chippy What if another application writes to the Console in the mean time? (which doesn't use this logger of course)Exposition

© 2022 - 2024 — McMap. All rights reserved.