Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Errors and warnings related to the
There are a few errors related to the lock statement and thread synchronization:
- CS0185: 'type' is not a reference type as required by the lock statement.
- CS1996: Cannot await in the body of a lock statement
- CS9217: A lock statement on a value of type 'System.Threading.Lock' cannot be used in async methods or async lambda expressions.
In addition, the compiler might produce the following warning related to lock statements and thread synchronization:
- CS9216: A value of type
System.Threading.Lockconverted to a different type will use likely unintended monitor-based locking inlockstatement.
lock statement errors
- CS0185: 'type' is not a reference type as required by the lock statement.
- CS1996: Cannot await in the body of a lock statement
- CS9217: A lock statement on a value of type 'System.Threading.Lock' cannot be used in async methods or async lambda expressions.
These errors indicate that your code violates rules regarding the lock the statement:
- The object being
locked must be a reference types. Value types aren't allowed. - An
awaitexpression can't be used in the scope of alockstatement. - The
lockstatement can't be used withasyncmethods or lambda expressions. For this error, you can replace the type of object locked with a different type. Thelockstatement uses the Monitor API.
You must update your code to follow the rules of the lock statement.
lock warning
- CS9216: A value of type
System.Threading.Lockconverted to a different type will use likely unintended monitor-based locking inlockstatement.
Beginning with C# 13, the lock generates specialized code when the target object is a System.Threading.Lock object. The compiler generates this warning when you're using a Lock object, but your code converts its type to something else. Therefore, the compiler generates the general locking code, not the locking code specific to the Lock type. For example:
object lockObject = new System.Threading.Lock();
lock (lockObject) // CS9216
{
// .. Your code
}
You should ensure the variable or expression represents the type of the Lock object.