CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
The TryEnter( method of the Monitor class is similar to the Enter() method in that it tries to acquire an exclusive lock on an object. However, it does not block like the Enter( method. If the thread enters successfully then the TryEnter( method will return true.
Two of the three overloads of TryEnter( take a timeout parameter representing the amount of time to wait for the lock. Let's see an example of how to use TryEnter( , MonitorTryEnter.cs:
using System;One possible output from MonitorTryEnter is:
TryEnter( is useful in situations where contention is likely to occur and you don't want to put the thread sleep for an unspecified period of time. A good example of this is dialing in to an ISP. Assume there are two applications A and B that both want to dial in to an ISP using the same modem. There is only one modem connection available and once the connection is established, we do not know how much time the connected application will stay connected. Suppose application A dials the ISP first and after some time application B wants to dial; there is no point in application B waiting indefinitely, as we don't know how long application A will remain connected. In this case, application B could use TryEnter( to determine whether the modem is already locked by anyother application (A in this case), rather than waiting indefinitely using the Enter() method.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 660
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved