简体   繁体   English

代码抛出 Object 同步方法是从未同步的代码块中调用的

[英]code throws Object synchronization method was called from an unsynchronized block of code

I'm writing quite simple synchronized code and I'm really stuck on this error I get.我正在编写非常简单的同步代码,但我真的陷入了这个错误。 What could cause the error?什么可能导致错误?

Program throws error at last line on PulseAll(processingText)程序在 PulseAll(processingText) 的最后一行抛出错误

UPDATE: there is also other function in my monitoring class which is called by main thread ProcessingText(), could it cause the issue?更新:在我的监控 class 中还有其他 function 由主线程 ProcessingText() 调用,它会导致问题吗?

public void doWork(int threadTaskNumber)
        {
            while (!(bool)WorkDone)
            {
                lock (processingText)
                {
                    while (vowelCounter != 3 && threadTaskNumber != 0)
                    {
                        Monitor.Wait(processingText);
                    }

                    if (!(bool)workDone)
                    {

                        switch (threadTaskNumber)
                        {

                            case 0:
                                processingText += 'A';

                                if (vowelCounter != 3)
                                    vowelCounter++;
                                break;
                            case 1:
                                processingText += 'B';
                                break;
                            case 2:
                                processingText += 'C';
                                break;
                        }

                        if (++threadCounter == 15)
                            WorkDone = true;
                    }

                    Monitor.PulseAll(processingText);
                }
            }
        }

public void ProcessingText()
        {
            lock (processingText)
            {
                Console.WriteLine(processingText);
            }
        }

The SynchronizationLockException is thrown because between these two lines:抛出SynchronizationLockException是因为在这两行之间:

Monitor.Wait(processingText);
//...
Monitor.PulseAll(processingText);

...the value of the variable processingText has changed. ...变量processingText的值已更改。 So the Monitor.PulseAll tries to send a pulse using a lock that is not owned by the current thread, hence the exception you observe.因此Monitor.PulseAll尝试使用当前线程不拥有的锁发送脉冲,因此您观察到异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常:从未同步的代码块中调用了对象同步方法 - Exception : Object synchronization method was called from an unsynchronized block of code 从一个未同步的代码块中调用了对象同步方法 - Object synchronization method was called from an unsynchronized block of code Unity Container-从未同步的代码块调用对象同步方法 - Unity Container - Object synchronization method was called from an unsynchronized block of code 从不同步的代码块调用Monitor和Object同步方法 - Monitor and Object synchronization method was called from an unsynchronized block of code 使用Mutex同步C#对象:“从未同步的代码块调用了对象同步方法”错误 - Use Mutex to synchronize C# object: “Object synchronization method was called from an unsynchronized block of code” error MVC中的异常“对象同步方法是从未同步的代码块中调用的。” - Exception “Object synchronization method was called from an unsynchronized block of code.” in MVC 使用监视器和锁“从未同步的代码块调用对象同步方法” - “Object synchronization method was called from an unsynchronized block of code” using monitors and lock 为什么这个程序出错? `对象同步方法是从一个不同步的代码块调用的 - Why is this program in error? `Object synchronization method was called from an unsynchronized block of code` 回调测试中的“从不同步的代码块调用对象同步方法”错误 - "Object synchronization method was called from an unsynchronized block of code" error in Callback test 在释放锁时,SynchronizationLockException(从未同步的代码块调用对象同步方法。) - SynchronizationLockException (Object synchronization method was called from an unsynchronized block of code.) when releasing a lock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM