SortedList.Synchronized 方法

返回 SortedList 的同步(线程安全)包装。

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function Synchronized ( _
    list As SortedList _
) As SortedList
用法
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

参数

返回值

SortedList 的同步(线程安全)包装。

异常

异常类型 条件

ArgumentNullException

list 为 空引用(在 Visual Basic 中为 Nothing)。

备注

提示

应用于此方法的 HostProtectionAttribute 属性 (attribute) 拥有以下 Resources 属性 (property) 值:SynchronizationHostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

若要保证 SortedList 的线程安全,必须通过此包装执行所有操作。

通过集合枚举在本质上不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。

示例

下面的代码示例演示如何在整个枚举过程中使用 SyncRoot 锁定集合。

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

此方法的运算复杂度为 O(1)。

下面的示例演示如何同步 SortedList、如何确定 SortedList 是否同步以及如何使用同步的 SortedList

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.
 */

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

SortedList 类
SortedList 成员
System.Collections 命名空间
IsSynchronized
SyncRoot