Partager via


SortedList.IsSynchronized Propriété

Définition

Obtient une valeur indiquant si l’accès à un SortedList objet est synchronisé (thread safe).

public:
 virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean

Valeur de propriété

true si l’accès à l’objet SortedList est synchronisé (thread safe) ; sinon, false. La valeur par défaut est false.

Implémente

Exemples

L’exemple de code suivant montre comment verrouiller une collection à l’aide de la SyncRoot propriété pendant toute l’énumération.

SortedList myCollection = new SortedList();
lock (myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As New SortedList()
SyncLock myCollection.SyncRoot
    For Each item In myCollection
        ' Insert your code here.
    Next item
End SyncLock

La récupération de la valeur de cette propriété est une O(1) opération.

L’exemple de code suivant montre comment synchroniser un SortedList objet, déterminer si un SortedList objet est synchronisé et utiliser un objet synchronisé SortedList.

using System;
using System.Collections;

public class SamplesSortedList
{
    public static void Main()
    {
        // Creates and initializes a new SortedList.
        SortedList mySL = new SortedList();
        mySL.Add(2, "two");
        mySL.Add(3, "three");
        mySL.Add(1, "one");
        mySL.Add(0, "zero");
        mySL.Add(4, "four");

        // Creates a synchronized wrapper around the SortedList.
        SortedList mySyncdSL = SortedList.Synchronized(mySL);

        // Displays the sychronization status of both SortedLists.
        Console.WriteLine("mySL is {0}.", mySL.IsSynchronized ? "synchronized" : "not synchronized");
        Console.WriteLine("mySyncdSL is {0}.", mySyncdSL.IsSynchronized ? "synchronized" : "not synchronized");
    }
}
/*
This code produces the following output.

mySL is not synchronized.
mySyncdSL is synchronized.
*/
Imports System.Collections

Public Class SamplesSortedList
    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add(2, "two")
        mySL.Add(3, "three")
        mySL.Add(1, "one")
        mySL.Add(0, "zero")
        mySL.Add(4, "four")
        
        ' Creates a synchronized wrapper around the SortedList.
        Dim mySyncdSL As SortedList = SortedList.Synchronized(mySL)
        
        ' Displays the sychronization status of both SortedLists.
        Dim msg As String
        If mySL.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySL is {0}.", msg)
        If mySyncdSL.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySyncdSL is {0}.", msg)        
    End Sub
End Class

' This code produces the following output.
'
' mySL is not synchronized.
' mySyncdSL is synchronized.

Remarques

Pour garantir la sécurité du thread d’un SortedList objet, toutes les opérations doivent être effectuées via le wrapper retourné par la Synchronized méthode.

L’énumération par le biais d’une collection n’est intrinsèquement pas une procédure thread-safe. Même lorsqu’une collection est synchronisée, d’autres threads peuvent toujours modifier la collection, ce qui provoque la levée d’une exception par l’énumérateur. Pour garantir la sécurité des threads pendant l’énumération, vous pouvez verrouiller la collection pendant toute l’énumération ou intercepter les exceptions résultant des modifications apportées par d’autres threads.

S’applique à

Voir aussi