Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
lock(obj){ // only 1 thread executes this at a time}
Monitor.Enter(obj); try { // only 1 thread executes this at a time } finally { Monitor.Exit(obj); }
bool taken = false; try { Monitor.Enter(obj, ref taken); // only 1 thread executes this at a time } finally { if (taken) { Monitor.Exit(obj); } }