V
V
VileMan2017-10-08 15:56:54
IT education
VileMan, 2017-10-08 15:56:54

C# why does the "called object has been disconnected from clients" error occur?

Good day!
While studying the manual on the basics of operating systems, I came across a problem that occurs when using lock statements; monitor.enter()..., ; etc.
More specifically

pikcha
59da1fde74321871344349.png

I dug for a long time in search of an answer (I'm a beginner myself and I don't have enough experience to figure out this problem) I'm
attaching the code ...
source
class Program
    {
        static int Runs = 0;
        static object block = new object();
        static void Main()
        {
            Thread t2 = new Thread(new ThreadStart(Method))
            {
                Name = "Thread 2"
            };
            Thread t3 = new Thread(new ThreadStart(Method))
            {
                Name = "Thread 3"
            };
            t2.Start();
            t3.Start();
            Console.ReadLine();
            

        }
        public static void Method()
        {
            try
            {
                Monitor.Enter(block);
                int Temp = Runs;
                Temp++;
                Console.WriteLine(Thread.CurrentThread.Name + " {0}", Temp);
                Thread.Sleep(1000);
                Runs = Temp;
            }
            finally
            {
                Monitor.Exit(block);
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2017-10-09
@VileMan

Your code worked fine for me.
Look for the problem in the studio, rights. Try running the studio with admin rights.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question