Monitor.TryEnter Method (Object, TimeSpan)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- obj
- Type: System.Object
The object on which to acquire the lock.
- timeout
- Type: System.TimeSpan
A TimeSpan representing the amount of time to wait for the lock. A value of –1 millisecond specifies an infinite wait.
Return Value
Type: System.Booleantrue if the current thread acquires the lock without blocking; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | The obj parameter is null. |
| ArgumentOutOfRangeException | The value of timeout in milliseconds is negative and is not equal to Infinite (–1 millisecond), or is greater than MaxValue. |
If the value of the timeout parameter converted to milliseconds equals –1, this method is equivalent to Enter. If the value of timeout equals 0, this method is equivalent to TryEnter.
Note: |
|---|
Use Monitor to lock objects (that is, reference types), not value types. For details, see Enter and the conceptual topic Monitors. |
Version Notes
Windows Phone
When a user navigates away from a Windows Phone application, the application is typically put into a dormant state. When the user returns to a dormant application, the application automatically resumes. If the application is put into a dormant state while this API is being used, the API will not complete as expected. Applications should be designed to handle this possibility. For more information about the Windows Phone execution model, see Execution Model for Windows Phone.
Note: