Scrigroup - Documente si articole


HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The TryEnter() Method


+ Font mai mare | - Font mai mic

The TryEnter() Method

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;
using System.Threading;

namespace MonitorTryEnter

public void CriticalSection()


public static void Main()

One possible output from MonitorTryEnter is:

Thread 2 TryEnter Value True
Thread 3 TryEnter Value False 1 2
1 3
2 2
2 3
3 2
3 3

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: 612
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved