Example

 using System; using System.Threading; using System.Collections; namespace Samples { public class MonitorSample { private static Hashtable ht = new Hashtable(); public static void Add(object key, object value) { Monitor.Enter(ht); ht.Add(key, value); Thread.Sleep(1000); Monitor.Exit(ht); } public static bool AddNoWait(object key, object value) { if(Monitor.TryEnter(ht)) { ht.Add(key, value); Thread.Sleep(1000); Monitor.Exit(ht); return true; } return false; } public static bool AddWithWaitTime(object key, object value, int waitTime) { if(Monitor.TryEnter(ht, waitTime)) { ht.Add(key, value); Thread.Sleep(1000); Monitor.Exit(ht); return true; } return true; } public static void Display() { Monitor.Enter(ht); IDictionaryEnumerator ide = ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.