Compartilhar via


SortedList.Synchronized(SortedList) Método

Definição

Retorna um wrapper sincronizado (thread-safe) para um SortedList objeto.

public:
 static System::Collections::SortedList ^ Synchronized(System::Collections::SortedList ^ list);
public static System.Collections.SortedList Synchronized(System.Collections.SortedList list);
static member Synchronized : System.Collections.SortedList -> System.Collections.SortedList
Public Shared Function Synchronized (list As SortedList) As SortedList

Parâmetros

list
SortedList

O SortedList objeto a ser sincronizado.

Retornos

Um wrapper sincronizado (thread-safe) para o SortedList objeto.

Exceções

list é null.

Exemplos

O exemplo de código a seguir mostra como bloquear a coleção usando a SyncRoot propriedade durante toda a enumeração.

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

Esse método é uma O(1) operação.

O exemplo de código a seguir mostra como sincronizar um SortedList objeto, determinar se um SortedList é sincronizado e usar um sincronizado 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.

Comentários

Para garantir a segurança de thread de um SortedList objeto, todas as operações devem ser feitas somente por meio desse wrapper.

Enumerar por meio de uma coleção não é intrinsecamente um procedimento thread-safe. Mesmo quando uma coleção é sincronizada, outros threads ainda podem modificar a coleção, o que faz com que o enumerador gere uma exceção. Para garantir a segurança do thread durante a enumeração, você pode bloquear a coleção durante toda a enumeração ou capturar as exceções resultantes de alterações feitas por outros threads.

Aplica-se a

Confira também