class MyClass { private object myLock = new object(); public void MyMethod() { lock (myLock) { // code that should only be accessed by one thread at a time } } }
class MyClass : IDisposable { private IntPtr handle; // unmanaged resource public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { // dispose of managed resources } // dispose of unmanaged resources } }The Lock and Dispose examples are both part of the .NET Framework's "System.Threading" namespace. Package/library: System.Threading.