SortedList.Synchronized-Methode
Gibt einen synchronisierten (threadsicheren) Wrapper für SortedList zurück.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Function Synchronized ( _
list As SortedList _
) As SortedList
'Usage
Dim list As SortedList
Dim returnValue As SortedList
returnValue = SortedList.Synchronized(list)
public static SortedList Synchronized (
SortedList list
)
public:
static SortedList^ Synchronized (
SortedList^ list
)
public static SortedList Synchronized (
SortedList list
)
public static function Synchronized (
list : SortedList
) : SortedList
Parameter
- list
Die SortedList, die synchronisiert werden soll.
Rückgabewert
Ein synchronisierter (threadsicherer) Wrapper für SortedList.
Ausnahmen
Ausnahmetyp | Bedingung |
---|---|
list ist NULL (Nothing in Visual Basic). |
Hinweise
Hinweis
Das auf diese Methode angewendete HostProtectionAttribute-Attribut besitzt den Resources-Eigenschaftenwert Synchronization. Das HostProtectionAttribute hat keine Auswirkungen auf Desktopanwendungen, die normalerweise durch Doppelklicken auf ein Symbol, Eingeben eines Befehls oder eines URL in einem Browser gestartet werden. Weitere Informationen finden Sie unter der HostProtectionAttribute-Klasse oder unter SQL Server-Programmierung und Hostschutzattribute.
Zur Gewährleistung der Threadsicherheit der SortedList müssen alle Operationen unter Verwendung dieses Wrappers ausgeführt werden.
Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert ist, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Sie können während der Enumeration Threadsicherheit gewährleisten, indem Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen abfangen, die durch Änderungen ausgelöst werden, die von anderen Threads vorgenommen werden.
Beispiel
Das folgende Codebeispiel veranschaulicht, wie die Auflistung mithilfe von SyncRoot 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()
Dim item As Object
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next item
End SyncLock
Diese Methode ist eine O(1)-Operation.
Im folgenden Beispiel wird gezeigt, wie Sie eine SortedList synchronisieren, eine synchronisierte SortedList verwenden und wie Sie ermitteln, ob eine SortedList synchronisiert ist.
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
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.
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.
*/
#using <system.dll>
using namespace System;
using namespace System::Collections;
int main()
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew SortedList;
mySL->Add( 2, "two" );
mySL->Add( 3, "three" );
mySL->Add( 1, "one" );
mySL->Add( (int^)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 ? (String^)"synchronized" : "not synchronized" );
Console::WriteLine( "mySyncdSL is {0}.", mySyncdSL->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}
/*
This code produces the following output.
mySL is not synchronized.
mySyncdSL is synchronized.
*/
import System.*;
import System.Collections.*;
public class SamplesSortedList
{
public static void main(String[] args)
{
// Creates and initializes a new SortedList.
SortedList mySL = new SortedList();
mySL.Add((Int32)2, "two");
mySL.Add((Int32)3, "three");
mySL.Add((Int32)1, "one");
mySL.Add((Int32)0, "zero");
mySL.Add((Int32)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.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
Console.WriteLine("mySyncdSL is {0}.",
(mySyncdSL.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
} //main
} //SamplesSortedList
/*
This code produces the following output.
mySL is not synchronized.
mySyncdSL is synchronized.
*/
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0
Siehe auch
Referenz
SortedList-Klasse
SortedList-Member
System.Collections-Namespace
IsSynchronized
SyncRoot