Hashtable.IsSynchronized プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)


Hashtable では、1 つの書き込み操作と複数の読み込み操作が同時に発生しても問題ありません。しかし、複数の書き込み操作をサポートするには、すべての操作が Synchronized メソッドから返されるラッパー経由で実行される必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。
列挙処理中に SyncRoot を使用してコレクションをロックする方法を次のコード例に示します。
Hashtable myCollection = new Hashtable(); lock(myCollection.SyncRoot) { foreach (Object item in myCollection) { // Insert your code here. } }

Hashtable を同期する方法と、Hashtable が同期されているかどうかを確認し、同期済みの Hashtable を使用する方法の例を次に示します。
Imports System Imports System.Collections Public Class SamplesHashtable Public Shared Sub Main() ' Creates and initializes a new Hashtable. Dim myHT As New Hashtable() myHT.Add(0, "zero") myHT.Add(1, "one") myHT.Add(2, "two") myHT.Add(3, "three") myHT.Add(4, "four") ' Creates a synchronized wrapper around the Hashtable. Dim mySyncdHT As Hashtable = Hashtable.Synchronized(myHT) ' Displays the sychronization status of both Hashtables. Dim msg As String If myHT.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("myHT is {0}.", msg) If mySyncdHT.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("mySyncdHT is {0}.", msg) End Sub End Class ' This code produces the following output. ' ' myHT is not synchronized. ' mySyncdHT is synchronized.
using System; using System.Collections; public class SamplesHashtable { public static void Main() { // Creates and initializes a new Hashtable. Hashtable myHT = new Hashtable(); myHT.Add( 0, "zero" ); myHT.Add( 1, "one" ); myHT.Add( 2, "two" ); myHT.Add( 3, "three" ); myHT.Add( 4, "four" ); // Creates a synchronized wrapper around the Hashtable. Hashtable mySyncdHT = Hashtable.Synchronized( myHT ); // Displays the sychronization status of both Hashtables. Console.WriteLine( "myHT is {0}.", myHT.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdHT is {0}.", mySyncdHT.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myHT is not synchronized. mySyncdHT is synchronized. */
#using <system.dll> using namespace System; using namespace System::Collections; void main() { // Creates and initializes a new Hashtable. Hashtable^ myHT = gcnew Hashtable; myHT->Add( (int^)0, "zero" ); myHT->Add( 1, "one" ); myHT->Add( 2, "two" ); myHT->Add( 3, "three" ); myHT->Add( 4, "four" ); // Creates a synchronized wrapper around the Hashtable. Hashtable^ mySyncdHT = Hashtable::Synchronized( myHT ); // Displays the sychronization status of both Hashtables. Console::WriteLine( "myHT is {0}.", myHT->IsSynchronized ? (String^)"synchronized" : "not synchronized" ); Console::WriteLine( "mySyncdHT is {0}.", mySyncdHT->IsSynchronized ? (String^)"synchronized" : "not synchronized" ); } /* This code produces the following output. myHT is not synchronized. mySyncdHT is synchronized. */
import System.*; import System.Collections.*; public class SamplesHashtable { public static void main(String[] args) { // Creates and initializes a new Hashtable. Hashtable myHT = new Hashtable(); myHT.Add(new Integer(0), "zero"); myHT.Add(new Integer(1), "one"); myHT.Add(new Integer(2), "two"); myHT.Add(new Integer(3), "three"); myHT.Add(new Integer(4), "four"); // Creates a synchronized wrapper around the Hashtable. Hashtable mySyncdHT = Hashtable.Synchronized(myHT); // Displays the sychronization status of both Hashtables. Console.WriteLine("myHT is {0}.", (myHT.get_IsSynchronized()) ? "synchronized" : "not synchronized"); Console.WriteLine("mySyncdHT is {0}.", (mySyncdHT.get_IsSynchronized()) ? "synchronized" : "not synchronized"); } //main } //SamplesHashtable /* This code produces the following output. myHT is not synchronized. mySyncdHT is synchronized. */
import System import System.Collections // Creates and initializes a new Hashtable. var myHT : Hashtable = new Hashtable() myHT.Add(0, "zero") myHT.Add(1, "one") myHT.Add(2, "two") myHT.Add(3, "three") myHT.Add(4, "four") // Creates a synchronized wrapper around the Hashtable. var mySyncdHT : Hashtable = Hashtable.Synchronized(myHT) // Displays the sychronization status of both Hashtables. var msg : String if(myHT.IsSynchronized) msg = "synchronized" else msg = "not synchronized" Console.WriteLine("myHT is {0}.", msg) if(mySyncdHT.IsSynchronized) msg = "synchronized" else msg = "not synchronized" Console.WriteLine("mySyncdHT is {0}.", msg) // This code produces the following output. // // myHT is not synchronized. // mySyncdHT 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からHashtable.IsSynchronized プロパティを検索する場合は、下記のリンクをクリックしてください。

- Hashtable.IsSynchronized プロパティのページへのリンク