다음을 통해 공유


Monitor.TryEnter Method (Object, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified object.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function TryEnter ( _
    obj As Object, _
    millisecondsTimeout As Integer _
) As Boolean
public static bool TryEnter(
    Object obj,
    int millisecondsTimeout
)

Parameters

  • obj
    Type: System.Object
    The object on which to acquire the lock.
  • millisecondsTimeout
    Type: System.Int32
    The number of milliseconds to wait for the lock.

Return Value

Type: System.Boolean
true if the current thread acquires the lock; otherwise, false.

Exceptions

Exception Condition
ArgumentNullException

The obj parameter is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

millisecondsTimeout is negative, and not equal to Infinite.

Remarks

If the millisecondsTimeout parameter equals Infinite, this method is equivalent to Enter. If millisecondsTimeout equals 0, this method is equivalent to TryEnter.

NoteNote:

Use Monitor to lock objects (that is, reference types), not value types. For details, see Enter and the conceptual topic Monitors.

Version Notes

Silverlight for Windows Phone Silverlight for 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.

Examples

The following example demonstrates how to use the TryEnter(Object, Int32) method overload with a time-out. This code is part of a larger example provided for the Enter method. The example compares this overload of TryEnter with the overload that does not have a time-out; using the time-out significantly increases the percentage of times that TryEnter succeeds in acquiring the lock.

' Try to add an element to the queue: Add the element to the queue 
' only if the lock becomes available during the specified time
' interval.
Public Function TryEnqueue(ByVal qValue As T, ByVal waitTime As Integer) As Boolean

   ' Request the lock.
   If Monitor.TryEnter(m_inputQueue, waitTime) Then
      Try
         m_inputQueue.Enqueue(qValue)

      Finally
         ' Ensure that the lock is released.
         Monitor.Exit(m_inputQueue)
      End Try
      Return True
   Else
      Return False
   End If
End Function
// Try to add an element to the queue: Add the element to the queue 
// only if the lock becomes available during the specified time
// interval.
public bool TryEnqueue(T qValue, int waitTime)
{
   // Request the lock.
   if (Monitor.TryEnter(m_inputQueue, waitTime))
   {
      try
      {
         m_inputQueue.Enqueue(qValue);
      }
      finally
      {
         // Ensure that the lock is released.
         Monitor.Exit(m_inputQueue);
      }
      return true;
   }
   else
   {
      return false;
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.