Answer the question
In order to leave comments, you need to log in
Creating a lock object?
hello everyone, here is an example:
class Program
{
static object locker = new object();
static void WriteSecond()
{
for (int i = 0; i < 20; i++)
{
lock (locker)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(new string(' ', 10) + "Secondary");
Console.ForegroundColor = ConsoleColor.Gray;
Thread.Sleep(100);
}
}
}
static void Main()
{
Console.SetWindowSize(80, 45);
ThreadStart writeSecond = new ThreadStart(WriteSecond);
Thread thread = new Thread(writeSecond);
thread.Start();
for (int i = 0; i < 20; i++)
{
lock (locker)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Primary");
Console.ForegroundColor = ConsoleColor.Gray;
Thread.Sleep(100);
}
}
// Delay.
Console.ReadKey();
}
}
Answer the question
In order to leave comments, you need to log in
If the same object is locked in different classes, then yes, such a division is possible.
In a comment to the question , Michael already indicated how this can be done.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question