Console.ForegroundColor per thread getting jumbled.

  • Thread starter Thread starter Aju
  • Start date Start date
A

Aju

lock (o)
{
Console.ForegroundColor =
(ConsoleColor)Enum.Parse(typeof(ConsoleColor),
Attributes["ConsoleColor"]);
Console.Write(message);
Console.ResetColor();
}

I hoped each thread would get it's own color based on an external
parameter that's set at load time. However, ocassionlly outputs means
to be in green are coming out in red and so on.

Any ideas?
 
Well, I was going to say "add some locking"... so: what is "o"
here...? not everything is suitable for locking... and it must be the
same reference over all threads - the following would suffice:

public static class SharedLock {
public static readonly object Lock = new object();
}

then lock(SharedLock.Lock) {...}

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top