Hashtable.Synchronized メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim table As Hashtable Dim returnValue As Hashtable returnValue = Hashtable.Synchronized(table)
戻り値
Hashtable 用の同期された (スレッド セーフな) ラッパー。


![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
Hashtable を読み取るスレッドが存在しない場合、Synchronized は複数の書き込みスレッドをサポートします。1 つ以上の読み取りスレッドと 1 つ以上の書き込みスレッドが存在する場合、同期ラッパーはスレッド セーフなアクセスを提供しません。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。
列挙処理中に 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


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