If you place the attribute before a function and supply the MethodImplOptions. Synchronized enumeration in the constructor, the entire method will be synchronized when called. The compiler will create output that wraps the whole function, SynchronizedMethodName, in a Monitor. Enter and Monitor. Exit block. When a thread starts to enter the method it will acquire a lock. Upon exiting the method, it will release the lock. Here is an example of using the attribute.
"<" METHODIMPLATTRIBUTE(METHODIMPLOPTIONS.SYNCHRONIZED) ">"
Private Sub SynchronizedMethodName()
End Sub
This attribute should only be used when an entire function needs to be synchronized, so it is rarely used. If you can exit the synchronized block of code before the end of the method or wait to enter it to the middle of the method, Monitor should be used, as the attribute would waste processing cycles by locking the whole method and not just what needs to be synchronized.
Note: There are two SynchronizationAttribute classes. One is in
System.EnterpriseServices and the other is in
System.Runtime.Remoting.Contexts. The first can only be used on
classes derived from ServicedComponent. The second can only be used on
classes derived from ContextBoundObject.
No comments:
Post a Comment