SortedList.Synchronized メソッド
アセンブリ: 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 )
戻り値
SortedList 用の同期された (スレッド セーフな) ラッパー。


![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization です。HostProtectionAttribute は、デスクトップ アプリケーション (通常、アイコンのダブルクリック、コマンドの入力、またはブラウザへの 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
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SortedList.Synchronized メソッドのページへのリンク