Monitor.TryEnter Method (Object)
Attempts to acquire an exclusive lock on the specified object.
Assembly: mscorlib (in mscorlib.dll)
If successful, this method acquires an exclusive lock on the obj parameter. This method returns immediately, whether or not the lock is available.
This method is similar to Enter, but it will never block the current thread. If the thread cannot enter without blocking, the method returns false,.
To ensure that the thread does not enter the critical section, you should examine the method's return value and execute code in the critical section only if its return value is true. The following code fragment shows the pattern used to call this method. Note that you should call Exit in a finally block to ensure that the calling thread releases its lock on the critical section if an exception occurs.
Available since 8
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1