An exception occurs when you use Monitor. Exit to lock an object.

Source: Internet
Author: User
Lock (OBJ) {console. writeline (system. datetime. now); // monitor. exit (OBJ); thread. sleep (1, 3000); console. writeline (system. datetime. now );}

The code above indicates that if the monitor. Exit (OBJ); Code is not deregistered, the error "the object synchronization method is called from the unsynchronized code block" will appear,

The current Code environment is net 3.5 SP1, and lock is equivalent

Monitor. Enter (OBJ); try {monitor. Exit (OBJ) ;}finally {monitor. Exit (OBJ );}

The reason for the test is that I execute monitor in try. after exit (OBJ);, OBJ has been released, and finally runs monitor again. exit (OBJ);, because the current thread object lock has been released, an exception is thrown. Similarly, the monitor. tryenter (OBJ); continue to execute monitor no matter the result is false. exit (OBJ); this will also cause the error of "Object synchronization method called from the unsynchronized code block ".

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.