Export (0) Print
Expand All

SortedList.Synchronized Method

Returns a synchronized (thread-safe) wrapper for the SortedList.

[Visual Basic]
Public Shared Function Synchronized( _
   ByVal list As SortedList _
) As SortedList
[C#]
public static SortedList Synchronized(
 SortedList list
);
[C++]
public: static SortedList* Synchronized(
 SortedList* list
);
[JScript]
public static function Synchronized(
   list : SortedList
) : SortedList;

Parameters

list
The SortedList to synchronize.

Return Value

A synchronized (thread-safe) wrapper for the SortedList.

Exceptions

Exception Type Condition
ArgumentNullException list is a null reference (Nothing in Visual Basic).

Remarks

To guarantee the thread safety of the SortedList, all operations must be done through this wrapper only.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

[Visual Basic, C#] The following code example shows how to lock the collection using the SyncRoot during the entire enumeration:

[C#] 
SortedList myCollection = new SortedList();
 lock( myCollection.SyncRoot ) {
 foreach ( Object item in myCollection ) {
 // Insert your code here.
 }
}
[Visual Basic] 
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

Example

[Visual Basic, C#, C++] The following example shows how to synchronize a SortedList, determine if a SortedList is synchronized and use a synchronized SortedList.

[Visual Basic] 
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.

[C#] 
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.
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

int main()  {

   // Creates and initializes a new SortedList.
   SortedList* mySL = new SortedList();
   mySL->Add( __box(2), S"two" );
   mySL->Add( __box(3), S"three" );
   mySL->Add( __box(1), S"one" );
   mySL->Add( __box(0), S"zero" );
   mySL->Add( __box(4), S"four" );

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

   // Displays the sychronization status of both SortedLists.
   Console::WriteLine( S"mySL is {0}.", mySL->IsSynchronized ? S"synchronized" : S"not synchronized" );
   Console::WriteLine( S"mySyncdSL is {0}.", mySyncdSL->IsSynchronized ? S"synchronized" : S"not synchronized" );
}

/*
This code produces the following output.

mySL is not synchronized.
mySyncdSL is synchronized.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

SortedList Class | SortedList Members | System.Collections Namespace | IsSynchronized | SyncRoot

Show:
© 2014 Microsoft