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 ".