Freigeben über


SortedList.Synchronized(SortedList) Methode

Definition

Gibt einen synchronisierten (threadsicheren) Wrapper für ein SortedList Objekt zurück.

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

Parameter

list
SortedList

Das SortedList zu synchronisierende Objekt.

Gibt zurück

Ein synchronisierter (threadsicherer) Wrapper für das SortedList Objekt.

Ausnahmen

list ist null.

Beispiele

Das folgende Codebeispiel zeigt, wie die Auflistung mithilfe der SyncRoot Eigenschaft während der gesamten Enumeration gesperrt wird.

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

Diese Methode ist ein O(1) Vorgang.

Im folgenden Codebeispiel wird gezeigt, wie ein SortedList Objekt synchronisiert wird, ob ein SortedList Objekt synchronisiert wird und ein synchronisiertes SortedListObjekt verwendet wird.

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.

Hinweise

Um die Threadsicherheit eines SortedList Objekts zu gewährleisten, müssen alle Vorgänge nur über diesen Wrapper ausgeführt werden.

Das Aufzählen durch eine Sammlung ist in erster Linie keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert wird, können andere Threads die Auflistung weiterhin ändern, wodurch der Enumerator eine Ausnahme auslöst. Um die Threadsicherheit während der Enumeration zu gewährleisten, können Sie die Auflistung entweder während der gesamten Enumeration sperren oder die Ausnahmen erfassen, die sich aus Änderungen ergeben, die von anderen Threads vorgenommen wurden.

Gilt für:

Weitere Informationen