Semaphore コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
Semaphore クラスの新しいインスタンスを初期化します。
オーバーロード
| 名前 | 説明 |
|---|---|
| Semaphore(Int32, Int32) |
エントリの初期数と同時実行エントリの最大数を指定して、 Semaphore クラスの新しいインスタンスを初期化します。 |
| Semaphore(Int32, Int32, String) |
エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前を指定して、 Semaphore クラスの新しいインスタンスを初期化します。 |
| Semaphore(Int32, Int32, String, Boolean) |
エントリの初期数と同時実行エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前を指定し、新しいシステム セマフォが作成されたかどうかを示す値を受け取る変数を指定して、 Semaphore クラスの新しいインスタンスを初期化します。 |
| Semaphore(Int32, Int32, String, NamedWaitHandleOptions) |
Semaphore クラスの新しいインスタンスを初期化し、エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前と、ユーザー スコープとセッション スコープのアクセスを設定するオプションを指定します。 |
| Semaphore(Int32, Int32, String, Boolean, SemaphoreSecurity) |
Semaphore クラスの新しいインスタンスを初期化し、エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前を指定し、新しいシステム セマフォが作成されたかどうかを示す値を受け取る変数を指定し、システム セマフォのセキュリティ アクセス制御を指定します。 |
| Semaphore(Int32, Int32, String, NamedWaitHandleOptions, Boolean) |
Semaphore クラスの新しいインスタンスを初期化します。エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前と、ユーザー スコープとセッション スコープのアクセスを設定するオプションを指定し、新しいシステム セマフォが作成されたかどうかを示す値を受け取る変数を指定します。 |
Semaphore(Int32, Int32)
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
エントリの初期数と同時実行エントリの最大数を指定して、 Semaphore クラスの新しいインスタンスを初期化します。
public:
Semaphore(int initialCount, int maximumCount);
public Semaphore(int initialCount, int maximumCount);
new System.Threading.Semaphore : int * int -> System.Threading.Semaphore
Public Sub New (initialCount As Integer, maximumCount As Integer)
パラメーター
- initialCount
- Int32
同時に許可できるセマフォの要求の初期数。
- maximumCount
- Int32
同時に許可できるセマフォの要求の最大数。
例外
initialCount が maximumCount より大きい。
例
次の例では、最大カウントが 3 で、初期カウントが 0 のセマフォを作成します。 この例では、セマフォの待機をブロックする 5 つのスレッドを開始します。 メイン スレッドは、 Release(Int32) メソッドのオーバーロードを使用してセマフォの数を最大値に増やし、3 つのスレッドがセマフォに入れるようにします。 各スレッドは、 Thread.Sleep メソッドを使用して 1 秒間待機し、作業をシミュレートした後、 Release() メソッドオーバーロードを呼び出してセマフォを解放します。 セマフォが解放されるたびに、前のセマフォ数が表示されます。 コンソール メッセージはセマフォの使用を追跡します。 シミュレートされた作業間隔は、出力を読みやすくするために、スレッドごとにわずかに増加します。
using System;
using System.Threading;
public class Example
{
// A semaphore that simulates a limited resource pool.
//
private static Semaphore _pool;
// A padding interval to make the output more orderly.
private static int _padding;
public static void Main()
{
// Create a semaphore that can satisfy up to three
// concurrent requests. Use an initial count of zero,
// so that the entire semaphore count is initially
// owned by the main program thread.
//
_pool = new Semaphore(initialCount: 0, maximumCount: 3);
// Create and start five numbered threads.
//
for(int i = 1; i <= 5; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(Worker));
// Start the thread, passing the number.
//
t.Start(i);
}
// Wait for half a second, to allow all the
// threads to start and to block on the semaphore.
//
Thread.Sleep(500);
// The main thread starts out holding the entire
// semaphore count. Calling Release(3) brings the
// semaphore count back to its maximum value, and
// allows the waiting threads to enter the semaphore,
// up to three at a time.
//
Console.WriteLine("Main thread calls Release(3).");
_pool.Release(releaseCount: 3);
Console.WriteLine("Main thread exits.");
}
private static void Worker(object num)
{
// Each worker thread begins by requesting the
// semaphore.
Console.WriteLine("Thread {0} begins " +
"and waits for the semaphore.", num);
_pool.WaitOne();
// A padding interval to make the output more orderly.
int padding = Interlocked.Add(ref _padding, 100);
Console.WriteLine("Thread {0} enters the semaphore.", num);
// The thread's "work" consists of sleeping for
// about a second. Each thread "works" a little
// longer, just to make the output more orderly.
//
Thread.Sleep(1000 + padding);
Console.WriteLine("Thread {0} releases the semaphore.", num);
Console.WriteLine("Thread {0} previous semaphore count: {1}",
num, _pool.Release());
}
}
Imports System.Threading
Public Class Example
' A semaphore that simulates a limited resource pool.
'
Private Shared _pool As Semaphore
' A padding interval to make the output more orderly.
Private Shared _padding As Integer
<MTAThread> _
Public Shared Sub Main()
' Create a semaphore that can satisfy up to three
' concurrent requests. Use an initial count of zero,
' so that the entire semaphore count is initially
' owned by the main program thread.
'
_pool = New Semaphore(0, 3)
' Create and start five numbered threads.
'
For i As Integer = 1 To 5
Dim t As New Thread(New ParameterizedThreadStart(AddressOf Worker))
'Dim t As New Thread(AddressOf Worker)
' Start the thread, passing the number.
'
t.Start(i)
Next i
' Wait for half a second, to allow all the
' threads to start and to block on the semaphore.
'
Thread.Sleep(500)
' The main thread starts out holding the entire
' semaphore count. Calling Release(3) brings the
' semaphore count back to its maximum value, and
' allows the waiting threads to enter the semaphore,
' up to three at a time.
'
Console.WriteLine("Main thread calls Release(3).")
_pool.Release(3)
Console.WriteLine("Main thread exits.")
End Sub
Private Shared Sub Worker(ByVal num As Object)
' Each worker thread begins by requesting the
' semaphore.
Console.WriteLine("Thread {0} begins " _
& "and waits for the semaphore.", num)
_pool.WaitOne()
' A padding interval to make the output more orderly.
Dim padding As Integer = Interlocked.Add(_padding, 100)
Console.WriteLine("Thread {0} enters the semaphore.", num)
' The thread's "work" consists of sleeping for
' about a second. Each thread "works" a little
' longer, just to make the output more orderly.
'
Thread.Sleep(1000 + padding)
Console.WriteLine("Thread {0} releases the semaphore.", num)
Console.WriteLine("Thread {0} previous semaphore count: {1}", _
num, _
_pool.Release())
End Sub
End Class
注釈
このコンストラクターは、名前のないセマフォを初期化します。 このようなセマフォのインスタンスを使用するすべてのスレッドには、そのインスタンスへの参照が必要です。
initialCountがmaximumCount未満の場合、効果は、現在のスレッドがWaitOne (maximumCount - initialCount) 回呼び出した場合と同じです。 セマフォを作成するスレッドのエントリを予約しない場合は、 maximumCount と initialCountに同じ数を使用します。
こちらもご覧ください
適用対象
Semaphore(Int32, Int32, String)
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前を指定して、 Semaphore クラスの新しいインスタンスを初期化します。
public:
Semaphore(int initialCount, int maximumCount, System::String ^ name);
public Semaphore(int initialCount, int maximumCount, string name);
public Semaphore(int initialCount, int maximumCount, string? name);
new System.Threading.Semaphore : int * int * string -> System.Threading.Semaphore
Public Sub New (initialCount As Integer, maximumCount As Integer, name As String)
パラメーター
- initialCount
- Int32
同時に許可できるセマフォの要求の初期数。
- maximumCount
- Int32
同時に許可できるセマフォの要求の最大数。
- name
- String
同期オブジェクトを他のプロセスと共有する場合の名前。それ以外の場合は、 null または空の文字列。 名前では大文字と小文字が区別されます。 円記号 (\) は予約されており、名前空間の指定にのみ使用できます。 名前空間の詳細については、「解説」セクションを参照してください。 オペレーティング システムによっては、名前にさらに制限がある場合があります。 たとえば、Unix ベースのオペレーティング システムでは、名前空間を除外した後の名前は有効なファイル名である必要があります。
例外
initialCount が maximumCount より大きい。
-又は-
.NET Framework のみ: name がMAX_PATH (260 文字) より長くなっています。
name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。
-又は-
その他のエラーが発生しました。
HResultプロパティは、詳細情報を提供する場合があります。
Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。
name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。
名前付きセマフォが存在し、アクセス制御セキュリティを持ち、ユーザーに FullControlがありません。
指定された name を持つ同期オブジェクトを作成できません。 異なる種類の同期オブジェクトの名前が同じである可能性があります。
例
次のコード例は、名前付きセマフォのクロスプロセス動作を示しています。 この例では、最大カウントが 5 で、初期カウントが 5 の名前付きセマフォを作成します。 プログラムは、 WaitOne メソッドを 3 回呼び出します。 したがって、2 つのコマンド ウィンドウからコンパイルされた例を実行すると、2 番目のコピーは、 WaitOneの 3 番目の呼び出しでブロックされます。 プログラムの最初のコピーで 1 つ以上のエントリを解放して、2 番目のエントリのブロックを解除します。
using System;
using System.Threading;
public class Example
{
public static void Main()
{
// Create a Semaphore object that represents the named
// system semaphore "SemaphoreExample3". The semaphore has a
// maximum count of five. The initial count is also five.
// There is no point in using a smaller initial count,
// because the initial count is not used if this program
// doesn't create the named system semaphore, and with
// this method overload there is no way to tell. Thus, this
// program assumes that it is competing with other
// programs for the semaphore.
//
Semaphore sem = new Semaphore(5, 5, "SemaphoreExample3");
// Attempt to enter the semaphore three times. If another
// copy of this program is already running, only the first
// two requests can be satisfied. The third blocks. Note
// that in a real application, timeouts should be used
// on the WaitOne calls, to avoid deadlocks.
//
sem.WaitOne();
Console.WriteLine("Entered the semaphore once.");
sem.WaitOne();
Console.WriteLine("Entered the semaphore twice.");
sem.WaitOne();
Console.WriteLine("Entered the semaphore three times.");
// The thread executing this program has entered the
// semaphore three times. If a second copy of the program
// is run, it will block until this program releases the
// semaphore at least once.
//
Console.WriteLine("Enter the number of times to call Release.");
int n;
if (int.TryParse(Console.ReadLine(), out n))
{
sem.Release(n);
}
int remaining = 3 - n;
if (remaining > 0)
{
Console.WriteLine("Press Enter to release the remaining " +
"count ({0}) and exit the program.", remaining);
Console.ReadLine();
sem.Release(remaining);
}
}
}
Imports System.Threading
Public Class Example
<MTAThread> _
Public Shared Sub Main()
' Create a Semaphore object that represents the named
' system semaphore "SemaphoreExample3". The semaphore has a
' maximum count of five. The initial count is also five.
' There is no point in using a smaller initial count,
' because the initial count is not used if this program
' doesn't create the named system semaphore, and with
' this method overload there is no way to tell. Thus, this
' program assumes that it is competing with other
' programs for the semaphore.
'
Dim sem As New Semaphore(5, 5, "SemaphoreExample3")
' Attempt to enter the semaphore three times. If another
' copy of this program is already running, only the first
' two requests can be satisfied. The third blocks. Note
' that in a real application, timeouts should be used
' on the WaitOne calls, to avoid deadlocks.
'
sem.WaitOne()
Console.WriteLine("Entered the semaphore once.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore twice.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore three times.")
' The thread executing this program has entered the
' semaphore three times. If a second copy of the program
' is run, it will block until this program releases the
' semaphore at least once.
'
Console.WriteLine("Enter the number of times to call Release.")
Dim n As Integer
If Integer.TryParse(Console.ReadLine(), n) Then
sem.Release(n)
End If
Dim remaining As Integer = 3 - n
If (remaining) > 0 Then
Console.WriteLine("Press Enter to release the remaining " _
& "count ({0}) and exit the program.", remaining)
Console.ReadLine()
sem.Release(remaining)
End If
End Sub
End Class
注釈
このコンストラクターは、名前付きシステム セマフォを表す Semaphore オブジェクトを初期化します。 同じ名前付きシステム セマフォを表す複数の Semaphore オブジェクトを作成できます。
nameには、名前空間を指定するために、Global\またはLocal\のプレフィックスを付ける場合があります。
Global名前空間を指定すると、同期オブジェクトをシステム上の任意のプロセスと共有できます。
Local名前空間が指定されている場合(名前空間が指定されていない場合の既定値)、同期オブジェクトを同じセッション内のプロセスと共有できます。 Windowsでは、セッションはログイン セッションであり、サービスは通常、別の非対話型セッションで実行されます。 Unix に似たオペレーティング システムでは、各シェルに独自のセッションがあります。 セッションとローカルの同期オブジェクトは、プロセス間の同期に適している場合があります。このオブジェクトはすべて同じセッションで実行されます。 Windowsの同期オブジェクト名の詳細については、「Object Names」を参照してください。
nameが指定されていて、要求された型の同期オブジェクトが名前空間に既に存在する場合は、既存の同期オブジェクトが使用されます。 別の型の同期オブジェクトが既に名前空間に存在する場合は、 WaitHandleCannotBeOpenedException がスローされます。 それ以外の場合は、新しい同期オブジェクトが作成されます。
名前付きシステム セマフォが存在しない場合は、 initialCount および maximumCountで指定された初期カウントと最大カウントで作成されます。 名前付きシステム セマフォが既に存在する場合、 initialCount と maximumCount は使用されませんが、無効な値を指定しても例外が発生します。 名前付きシステム セマフォが作成されたかどうかを判断する必要がある場合は、代わりに Semaphore(Int32, Int32, String, Boolean) コンストラクターのオーバーロードを使用します。
Important
このコンストラクター オーバーロードを使用する場合は、 initialCount と maximumCountに同じ数を指定することをお勧めします。
initialCountがmaximumCount未満で、名前付きシステム セマフォが作成された場合、効果は、現在のスレッドが WaitOne (maximumCount - initialCount) 回呼び出した場合と同じです。 ただし、このコンストラクターのオーバーロードでは、名前付きシステム セマフォが作成されたかどうかを判断する方法はありません。
nameにnullまたは空の文字列を指定すると、Semaphore(Int32, Int32) コンストラクターオーバーロードを呼び出したかのようにローカル セマフォが作成されます。
名前付きセマフォはオペレーティング システム全体で表示されるため、プロセス境界を越えてリソースの使用を調整するために使用できます。
名前付きシステム セマフォが存在するかどうかを調べるには、 OpenExisting メソッドを使用します。 OpenExisting メソッドは、既存の名前付きセマフォを開こうとし、システム セマフォが存在しない場合は例外をスローします。
Caution
既定では、名前付きセマフォは、それを作成したユーザーに制限されません。 セマフォを複数回取得して解放しないことでセマフォを妨害するなど、他のユーザーがセマフォを開いて使用できる場合があります。 特定のユーザーへのアクセスを制限するには、コンストラクターのオーバーロードまたは SemaphoreAcl を使用し、名前付きセマフォの作成時に SemaphoreSecurity を渡します。 信頼されていないユーザーがコードを実行している可能性があるシステムでは、アクセス制限なしで名前付きセマフォを使用しないでください。
こちらもご覧ください
適用対象
Semaphore(Int32, Int32, String, Boolean)
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
エントリの初期数と同時実行エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前を指定し、新しいシステム セマフォが作成されたかどうかを示す値を受け取る変数を指定して、 Semaphore クラスの新しいインスタンスを初期化します。
public:
Semaphore(int initialCount, int maximumCount, System::String ^ name, [Runtime::InteropServices::Out] bool % createdNew);
public Semaphore(int initialCount, int maximumCount, string name, out bool createdNew);
public Semaphore(int initialCount, int maximumCount, string? name, out bool createdNew);
new System.Threading.Semaphore : int * int * string * bool -> System.Threading.Semaphore
Public Sub New (initialCount As Integer, maximumCount As Integer, name As String, ByRef createdNew As Boolean)
パラメーター
- initialCount
- Int32
同時に満たすことができるセマフォの要求の初期数。
- maximumCount
- Int32
同時に満たすことができるセマフォの要求の最大数。
- name
- String
同期オブジェクトを他のプロセスと共有する場合の名前。それ以外の場合は、 null または空の文字列。 名前では大文字と小文字が区別されます。 円記号 (\) は予約されており、名前空間の指定にのみ使用できます。 名前空間の詳細については、「解説」セクションを参照してください。 オペレーティング システムによっては、名前にさらに制限がある場合があります。 たとえば、Unix ベースのオペレーティング システムでは、名前空間を除外した後の名前は有効なファイル名である必要があります。
- createdNew
- Boolean
このメソッドが戻るときに、ローカル セマフォが作成された場合 (つまり、nameがnullまたは空の文字列の場合) または指定した名前付きシステム セマフォが作成された場合、または指定した名前付きシステム セマフォが既に存在する場合はfalsetrueが含まれます。 このパラメーターは初期化せずに渡されます。
例外
initialCount が maximumCount より大きい。
-又は-
.NET Framework のみ: name がMAX_PATH (260 文字) より長くなっています。
name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。
-又は-
その他のエラーが発生しました。
HResultプロパティは、詳細情報を提供する場合があります。
Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。
name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。
名前付きセマフォが存在し、アクセス制御セキュリティを持ち、ユーザーに FullControlがありません。
指定された name を持つ同期オブジェクトを作成できません。 異なる種類の同期オブジェクトの名前が同じである可能性があります。
例
次のコード例は、名前付きセマフォのクロスプロセス動作を示しています。 この例では、最大カウントが 5 で、初期カウントが 2 の名前付きセマフォを作成します。 つまり、コンストラクターを呼び出すスレッドに対して 3 つのエントリが予約されます。
createNewがfalse場合、プログラムは WaitOne メソッドを 3 回呼び出します。 したがって、2 つのコマンド ウィンドウからコンパイルされた例を実行すると、2 番目のコピーは、 WaitOneの 3 番目の呼び出しでブロックされます。 プログラムの最初のコピーで 1 つ以上のエントリを解放して、2 番目のエントリのブロックを解除します。
using System;
using System.Threading;
public class Example
{
public static void Main()
{
// The value of this variable is set by the semaphore
// constructor. It is true if the named system semaphore was
// created, and false if the named semaphore already existed.
//
bool semaphoreWasCreated;
// Create a Semaphore object that represents the named
// system semaphore "SemaphoreExample". The semaphore has a
// maximum count of five, and an initial count of two. The
// Boolean value that indicates creation of the underlying
// system object is placed in semaphoreWasCreated.
//
Semaphore sem = new Semaphore(2, 5, "SemaphoreExample",
out semaphoreWasCreated);
if (semaphoreWasCreated)
{
// If the named system semaphore was created, its count is
// set to the initial count requested in the constructor.
// In effect, the current thread has entered the semaphore
// three times.
//
Console.WriteLine("Entered the semaphore three times.");
}
else
{
// If the named system semaphore was not created,
// attempt to enter it three times. If another copy of
// this program is already running, only the first two
// requests can be satisfied. The third blocks.
//
sem.WaitOne();
Console.WriteLine("Entered the semaphore once.");
sem.WaitOne();
Console.WriteLine("Entered the semaphore twice.");
sem.WaitOne();
Console.WriteLine("Entered the semaphore three times.");
}
// The thread executing this program has entered the
// semaphore three times. If a second copy of the program
// is run, it will block until this program releases the
// semaphore at least once.
//
Console.WriteLine("Enter the number of times to call Release.");
int n;
if (int.TryParse(Console.ReadLine(), out n))
{
sem.Release(n);
}
int remaining = 3 - n;
if (remaining > 0)
{
Console.WriteLine("Press Enter to release the remaining " +
"count ({0}) and exit the program.", remaining);
Console.ReadLine();
sem.Release(remaining);
}
}
}
Imports System.Threading
Public Class Example
<MTAThread> _
Public Shared Sub Main()
' The value of this variable is set by the semaphore
' constructor. It is True if the named system semaphore was
' created, and False if the named semaphore already existed.
'
Dim semaphoreWasCreated As Boolean
' Create a Semaphore object that represents the named
' system semaphore "SemaphoreExample". The semaphore has a
' maximum count of five, and an initial count of two. The
' Boolean value that indicates creation of the underlying
' system object is placed in semaphoreWasCreated.
'
Dim sem As New Semaphore(2, 5, "SemaphoreExample", _
semaphoreWasCreated)
If semaphoreWasCreated Then
' If the named system semaphore was created, its count is
' set to the initial count requested in the constructor.
' In effect, the current thread has entered the semaphore
' three times.
'
Console.WriteLine("Entered the semaphore three times.")
Else
' If the named system semaphore was not created,
' attempt to enter it three times. If another copy of
' this program is already running, only the first two
' requests can be satisfied. The third blocks.
'
sem.WaitOne()
Console.WriteLine("Entered the semaphore once.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore twice.")
sem.WaitOne()
Console.WriteLine("Entered the semaphore three times.")
End If
' The thread executing this program has entered the
' semaphore three times. If a second copy of the program
' is run, it will block until this program releases the
' semaphore at least once.
'
Console.WriteLine("Enter the number of times to call Release.")
Dim n As Integer
If Integer.TryParse(Console.ReadLine(), n) Then
sem.Release(n)
End If
Dim remaining As Integer = 3 - n
If (remaining) > 0 Then
Console.WriteLine("Press Enter to release the remaining " _
& "count ({0}) and exit the program.", remaining)
Console.ReadLine()
sem.Release(remaining)
End If
End Sub
End Class
注釈
nameには、名前空間を指定するために、Global\またはLocal\のプレフィックスを付ける場合があります。
Global名前空間を指定すると、同期オブジェクトをシステム上の任意のプロセスと共有できます。
Local名前空間が指定されている場合(名前空間が指定されていない場合の既定値)、同期オブジェクトを同じセッション内のプロセスと共有できます。 Windowsでは、セッションはログイン セッションであり、サービスは通常、別の非対話型セッションで実行されます。 Unix に似たオペレーティング システムでは、各シェルに独自のセッションがあります。 セッションとローカルの同期オブジェクトは、プロセス間の同期に適している場合があります。このオブジェクトはすべて同じセッションで実行されます。 Windowsの同期オブジェクト名の詳細については、「Object Names」を参照してください。
nameが指定されていて、要求された型の同期オブジェクトが名前空間に既に存在する場合は、既存の同期オブジェクトが使用されます。 別の型の同期オブジェクトが既に名前空間に存在する場合は、 WaitHandleCannotBeOpenedException がスローされます。 それ以外の場合は、新しい同期オブジェクトが作成されます。
このコンストラクターは、名前付きシステム セマフォを表す Semaphore オブジェクトを初期化します。 同じ名前付きシステム セマフォを表す複数の Semaphore オブジェクトを作成できます。
名前付きシステム セマフォが存在しない場合は、 initialCount および maximumCountで指定された初期カウントと最大カウントで作成されます。 名前付きシステム セマフォが既に存在する場合、 initialCount と maximumCount は使用されませんが、無効な値を指定しても例外が発生します。
createdNewを使用して、システム セマフォが作成されたかどうかを確認します。
initialCountがmaximumCount未満で、createdNewがtrue場合、現在のスレッドがWaitOne (maximumCount - initialCount) 回呼び出した場合と同じ効果が得られます。
nameにnullまたは空の文字列を指定すると、Semaphore(Int32, Int32) コンストラクターオーバーロードを呼び出したかのようにローカル セマフォが作成されます。 この場合、 createdNew は常に true。
名前付きセマフォはオペレーティング システム全体で表示されるため、プロセス境界を越えてリソースの使用を調整するために使用できます。
Caution
既定では、名前付きセマフォは、それを作成したユーザーに制限されません。 セマフォを複数回取得して解放しないことでセマフォを妨害するなど、他のユーザーがセマフォを開いて使用できる場合があります。 特定のユーザーへのアクセスを制限するには、コンストラクターのオーバーロードまたは SemaphoreAcl を使用し、名前付きセマフォの作成時に SemaphoreSecurity を渡します。 信頼されていないユーザーがコードを実行している可能性があるシステムでは、アクセス制限なしで名前付きセマフォを使用しないでください。
こちらもご覧ください
適用対象
Semaphore(Int32, Int32, String, NamedWaitHandleOptions)
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
Semaphore クラスの新しいインスタンスを初期化し、エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前と、ユーザー スコープとセッション スコープのアクセスを設定するオプションを指定します。
public:
Semaphore(int initialCount, int maximumCount, System::String ^ name, System::Threading::NamedWaitHandleOptions options);
public Semaphore(int initialCount, int maximumCount, string? name, System.Threading.NamedWaitHandleOptions options);
new System.Threading.Semaphore : int * int * string * System.Threading.NamedWaitHandleOptions -> System.Threading.Semaphore
Public Sub New (initialCount As Integer, maximumCount As Integer, name As String, options As NamedWaitHandleOptions)
パラメーター
- initialCount
- Int32
同時に満たすことができるセマフォの要求の初期数。
- maximumCount
- Int32
同時に満たすことができるセマフォの要求の最大数。
- name
- String
同期オブジェクトを他のプロセスと共有する場合の名前。それ以外の場合は、 null または空の文字列。 名前では大文字と小文字が区別されます。
- options
- NamedWaitHandleOptions
名前付きセマフォのスコープ オプション。 既定では、アクセスは現在のユーザーと現在のセッションのみに制限されます。 指定したオプションは、名前の名前空間と、基になるセマフォ オブジェクトへのアクセスに影響する可能性があります。
例外
name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。
-又は-
その他のエラーが発生しました。
HResultプロパティは、詳細情報を提供する場合があります。
Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。
name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。
名前付きセマフォが存在し、アクセス制御セキュリティを持ち、ユーザーに FullControlがありません。
指定された name を持つ同期オブジェクトを作成できません。 異なる種類の同期オブジェクトの名前が同じである可能性があります。
-又は-
指定した name を持つオブジェクトが存在しますが、指定した options は既存のオブジェクトのオプションと互換性がありません。
注釈
このコンストラクターは、名前付きシステム セマフォを表す Semaphore オブジェクトを初期化します。 同じ名前付きシステム セマフォを表す複数の Semaphore オブジェクトを作成できます。
nameが指定され、要求された型の同期オブジェクトが名前空間に既に存在する場合、optionsが現在のユーザーに制限されたアクセスを指定し、同期オブジェクトと互換性がない場合を除き、既存の同期オブジェクトが使用されます。この場合、WaitHandleCannotBeOpenedExceptionがスローされます。 別の型の同期オブジェクトが既に名前空間に存在する場合は、 WaitHandleCannotBeOpenedException もスローされます。 それ以外の場合は、新しい同期オブジェクトが作成されます。
名前付きシステム セマフォが存在しない場合は、 initialCount および maximumCountで指定された初期カウントと最大カウントで作成されます。 名前付きシステム セマフォが既に存在する場合、 initialCount と maximumCount は使用されませんが、無効な値を指定しても例外が発生します。 名前付きシステム セマフォが作成されたかどうかを判断する必要がある場合は、代わりに Semaphore(Int32, Int32, String, Boolean) コンストラクターのオーバーロードを使用します。
Important
このコンストラクター オーバーロードを使用する場合は、 initialCount と maximumCountに同じ数を指定することをお勧めします。
initialCountがmaximumCount未満で、名前付きシステム セマフォが作成された場合、効果は、現在のスレッドが WaitOne (maximumCount - initialCount) 回呼び出した場合と同じです。 ただし、このコンストラクターのオーバーロードでは、名前付きシステム セマフォが作成されたかどうかを判断する方法はありません。
nameにnullまたは空の文字列を指定すると、Semaphore(Int32, Int32) コンストラクターオーバーロードを呼び出したかのようにローカル セマフォが作成されます。
名前付きセマフォはオペレーティング システム全体で表示されるため、プロセス境界を越えてリソースの使用を調整するために使用できます。
名前付きシステム セマフォが存在するかどうかを調べるには、 OpenExisting メソッドを使用します。 OpenExisting メソッドは、既存の名前付きセマフォを開こうとし、システム セマフォが存在しない場合は例外をスローします。
Windowsでは、optionsを指定して、名前付きセマフォに現在のユーザーのみがアクセスできるか、すべてのユーザーにアクセスできるかを指定できます。 また、名前付きセマフォに現在のセッション内のプロセスのみにアクセスするか、すべてのセッションにアクセスできるかを指定することもできます。 詳細については、NamedWaitHandleOptionsを参照してください。
Caution
Unix ベースのオペレーティング システムでは、名前付きセマフォがサポートされていないため、 options パラメーターは無効です。
こちらもご覧ください
適用対象
Semaphore(Int32, Int32, String, Boolean, SemaphoreSecurity)
Semaphore クラスの新しいインスタンスを初期化し、エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前を指定し、新しいシステム セマフォが作成されたかどうかを示す値を受け取る変数を指定し、システム セマフォのセキュリティ アクセス制御を指定します。
public:
Semaphore(int initialCount, int maximumCount, System::String ^ name, [Runtime::InteropServices::Out] bool % createdNew, System::Security::AccessControl::SemaphoreSecurity ^ semaphoreSecurity);
public Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, System.Security.AccessControl.SemaphoreSecurity semaphoreSecurity);
new System.Threading.Semaphore : int * int * string * bool * System.Security.AccessControl.SemaphoreSecurity -> System.Threading.Semaphore
Public Sub New (initialCount As Integer, maximumCount As Integer, name As String, ByRef createdNew As Boolean, semaphoreSecurity As SemaphoreSecurity)
パラメーター
- initialCount
- Int32
同時に満たすことができるセマフォの要求の初期数。
- maximumCount
- Int32
同時に満たすことができるセマフォの要求の最大数。
- name
- String
同期オブジェクトを他のプロセスと共有する場合の名前。それ以外の場合は、 null または空の文字列。 名前では大文字と小文字が区別されます。 円記号 (\) は予約されており、名前空間の指定にのみ使用できます。 名前空間の詳細については、「解説」セクションを参照してください。 オペレーティング システムによっては、名前にさらに制限がある場合があります。 たとえば、Unix ベースのオペレーティング システムでは、名前空間を除外した後の名前は有効なファイル名である必要があります。
- createdNew
- Boolean
このメソッドが戻るときに、ローカル セマフォが作成された場合 (つまり、nameがnullまたは空の文字列の場合) または指定した名前付きシステム セマフォが作成された場合、または指定した名前付きシステム セマフォが既に存在する場合はfalsetrueが含まれます。 このパラメーターは初期化せずに渡されます。
- semaphoreSecurity
- SemaphoreSecurity
名前付きシステム セマフォに適用するアクセス制御セキュリティを表す SemaphoreSecurity オブジェクト。
例外
initialCount が maximumCount より大きい。
-又は-
.NET Framework のみ: name がMAX_PATH (260 文字) より長くなっています。
名前付きセマフォが存在し、アクセス制御セキュリティを持ち、ユーザーに FullControlがありません。
name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。
-又は-
その他のエラーが発生しました。
HResultプロパティは、詳細情報を提供する場合があります。
Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。
name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。
指定された name を持つ同期オブジェクトを作成できません。 異なる種類の同期オブジェクトの名前が同じである可能性があります。
例
次のコード例は、アクセス制御セキュリティを備えた名前付きセマフォのクロスプロセス動作を示しています。 この例では、 OpenExisting(String) メソッドのオーバーロードを使用して、名前付きセマフォの存在をテストします。 セマフォが存在しない場合は、セマフォを使用する権限を現在のユーザーに拒否し、セマフォに対するアクセス許可を読み取りおよび変更する権限を付与するアクセス制御セキュリティを使用して、セマフォが最大数 2 で作成されます。 2 つのコマンド ウィンドウからコンパイルされた例を実行した場合、2 番目のコピーでは、 OpenExisting(String) メソッドの呼び出しでアクセス違反の例外がスローされます。 例外がキャッチされ、この例では、 OpenExisting(String, SemaphoreRights) メソッドのオーバーロードを使用して、アクセス許可の読み取りと変更に必要な権限を持つセマフォを開きます。
アクセス許可が変更されると、入力と解放に必要な権限でセマフォが開かれます。 3 番目のコマンド ウィンドウからコンパイルされた例を実行すると、新しいアクセス許可を使用して実行されます。
using System;
using System.Threading;
using System.Security.AccessControl;
internal class Example
{
internal static void Main()
{
const string semaphoreName = "SemaphoreExample5";
Semaphore sem = null;
bool doesNotExist = false;
bool unauthorized = false;
// Attempt to open the named semaphore.
try
{
// Open the semaphore with (SemaphoreRights.Synchronize
// | SemaphoreRights.Modify), to enter and release the
// named semaphore.
//
sem = Semaphore.OpenExisting(semaphoreName);
}
catch(WaitHandleCannotBeOpenedException)
{
Console.WriteLine("Semaphore does not exist.");
doesNotExist = true;
}
catch(UnauthorizedAccessException ex)
{
Console.WriteLine("Unauthorized access: {0}", ex.Message);
unauthorized = true;
}
// There are three cases: (1) The semaphore does not exist.
// (2) The semaphore exists, but the current user doesn't
// have access. (3) The semaphore exists and the user has
// access.
//
if (doesNotExist)
{
// The semaphore does not exist, so create it.
//
// The value of this variable is set by the semaphore
// constructor. It is true if the named system semaphore was
// created, and false if the named semaphore already existed.
//
bool semaphoreWasCreated;
// Create an access control list (ACL) that denies the
// current user the right to enter or release the
// semaphore, but allows the right to read and change
// security information for the semaphore.
//
string user = Environment.UserDomainName + "\\"
+ Environment.UserName;
SemaphoreSecurity semSec = new SemaphoreSecurity();
SemaphoreAccessRule rule = new SemaphoreAccessRule(
user,
SemaphoreRights.Synchronize | SemaphoreRights.Modify,
AccessControlType.Deny);
semSec.AddAccessRule(rule);
rule = new SemaphoreAccessRule(
user,
SemaphoreRights.ReadPermissions | SemaphoreRights.ChangePermissions,
AccessControlType.Allow);
semSec.AddAccessRule(rule);
// Create a Semaphore object that represents the system
// semaphore named by the constant 'semaphoreName', with
// maximum count three, initial count three, and the
// specified security access. The Boolean value that
// indicates creation of the underlying system object is
// placed in semaphoreWasCreated.
//
sem = new Semaphore(3, 3, semaphoreName,
out semaphoreWasCreated, semSec);
// If the named system semaphore was created, it can be
// used by the current instance of this program, even
// though the current user is denied access. The current
// program enters the semaphore. Otherwise, exit the
// program.
//
if (semaphoreWasCreated)
{
Console.WriteLine("Created the semaphore.");
}
else
{
Console.WriteLine("Unable to create the semaphore.");
return;
}
}
else if (unauthorized)
{
// Open the semaphore to read and change the access
// control security. The access control security defined
// above allows the current user to do this.
//
try
{
sem = Semaphore.OpenExisting(
semaphoreName,
SemaphoreRights.ReadPermissions
| SemaphoreRights.ChangePermissions);
// Get the current ACL. This requires
// SemaphoreRights.ReadPermissions.
SemaphoreSecurity semSec = sem.GetAccessControl();
string user = Environment.UserDomainName + "\\"
+ Environment.UserName;
// First, the rule that denied the current user
// the right to enter and release the semaphore must
// be removed.
SemaphoreAccessRule rule = new SemaphoreAccessRule(
user,
SemaphoreRights.Synchronize | SemaphoreRights.Modify,
AccessControlType.Deny);
semSec.RemoveAccessRule(rule);
// Now grant the user the correct rights.
//
rule = new SemaphoreAccessRule(user,
SemaphoreRights.Synchronize | SemaphoreRights.Modify,
AccessControlType.Allow);
semSec.AddAccessRule(rule);
// Update the ACL. This requires
// SemaphoreRights.ChangePermissions.
sem.SetAccessControl(semSec);
Console.WriteLine("Updated semaphore security.");
// Open the semaphore with (SemaphoreRights.Synchronize
// | SemaphoreRights.Modify), the rights required to
// enter and release the semaphore.
//
sem = Semaphore.OpenExisting(semaphoreName);
}
catch(UnauthorizedAccessException ex)
{
Console.WriteLine("Unable to change permissions: {0}", ex.Message);
return;
}
}
// Enter the semaphore, and hold it until the program
// exits.
//
try
{
sem.WaitOne();
Console.WriteLine("Entered the semaphore.");
Console.WriteLine("Press the Enter key to exit.");
Console.ReadLine();
sem.Release();
}
catch(UnauthorizedAccessException ex)
{
Console.WriteLine("Unauthorized access: {0}", ex.Message);
}
}
}
Imports System.Threading
Imports System.Security.AccessControl
Friend Class Example
<MTAThread> _
Friend Shared Sub Main()
Const semaphoreName As String = "SemaphoreExample5"
Dim sem As Semaphore = Nothing
Dim doesNotExist as Boolean = False
Dim unauthorized As Boolean = False
' Attempt to open the named semaphore.
Try
' Open the semaphore with (SemaphoreRights.Synchronize
' Or SemaphoreRights.Modify), to enter and release the
' named semaphore.
'
sem = Semaphore.OpenExisting(semaphoreName)
Catch ex As WaitHandleCannotBeOpenedException
Console.WriteLine("Semaphore does not exist.")
doesNotExist = True
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", ex.Message)
unauthorized = True
End Try
' There are three cases: (1) The semaphore does not exist.
' (2) The semaphore exists, but the current user doesn't
' have access. (3) The semaphore exists and the user has
' access.
'
If doesNotExist Then
' The semaphore does not exist, so create it.
'
' The value of this variable is set by the semaphore
' constructor. It is True if the named system semaphore was
' created, and False if the named semaphore already existed.
'
Dim semaphoreWasCreated As Boolean
' Create an access control list (ACL) that denies the
' current user the right to enter or release the
' semaphore, but allows the right to read and change
' security information for the semaphore.
'
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
Dim semSec As New SemaphoreSecurity()
Dim rule As New SemaphoreAccessRule(user, _
SemaphoreRights.Synchronize Or SemaphoreRights.Modify, _
AccessControlType.Deny)
semSec.AddAccessRule(rule)
rule = New SemaphoreAccessRule(user, _
SemaphoreRights.ReadPermissions Or _
SemaphoreRights.ChangePermissions, _
AccessControlType.Allow)
semSec.AddAccessRule(rule)
' Create a Semaphore object that represents the system
' semaphore named by the constant 'semaphoreName', with
' maximum count three, initial count three, and the
' specified security access. The Boolean value that
' indicates creation of the underlying system object is
' placed in semaphoreWasCreated.
'
sem = New Semaphore(3, 3, semaphoreName, _
semaphoreWasCreated, semSec)
' If the named system semaphore was created, it can be
' used by the current instance of this program, even
' though the current user is denied access. The current
' program enters the semaphore. Otherwise, exit the
' program.
'
If semaphoreWasCreated Then
Console.WriteLine("Created the semaphore.")
Else
Console.WriteLine("Unable to create the semaphore.")
Return
End If
ElseIf unauthorized Then
' Open the semaphore to read and change the access
' control security. The access control security defined
' above allows the current user to do this.
'
Try
sem = Semaphore.OpenExisting(semaphoreName, _
SemaphoreRights.ReadPermissions Or _
SemaphoreRights.ChangePermissions)
' Get the current ACL. This requires
' SemaphoreRights.ReadPermissions.
Dim semSec As SemaphoreSecurity = sem.GetAccessControl()
Dim user As String = Environment.UserDomainName _
& "\" & Environment.UserName
' First, the rule that denied the current user
' the right to enter and release the semaphore must
' be removed.
Dim rule As New SemaphoreAccessRule(user, _
SemaphoreRights.Synchronize Or SemaphoreRights.Modify, _
AccessControlType.Deny)
semSec.RemoveAccessRule(rule)
' Now grant the user the correct rights.
'
rule = New SemaphoreAccessRule(user, _
SemaphoreRights.Synchronize Or SemaphoreRights.Modify, _
AccessControlType.Allow)
semSec.AddAccessRule(rule)
' Update the ACL. This requires
' SemaphoreRights.ChangePermissions.
sem.SetAccessControl(semSec)
Console.WriteLine("Updated semaphore security.")
' Open the semaphore with (SemaphoreRights.Synchronize
' Or SemaphoreRights.Modify), the rights required to
' enter and release the semaphore.
'
sem = Semaphore.OpenExisting(semaphoreName)
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unable to change permissions: {0}", _
ex.Message)
Return
End Try
End If
' Enter the semaphore, and hold it until the program
' exits.
'
Try
sem.WaitOne()
Console.WriteLine("Entered the semaphore.")
Console.WriteLine("Press the Enter key to exit.")
Console.ReadLine()
sem.Release()
Catch ex As UnauthorizedAccessException
Console.WriteLine("Unauthorized access: {0}", _
ex.Message)
End Try
End Sub
End Class
注釈
このコンストラクターを使用して、名前付きシステム セマフォの作成時にアクセス制御セキュリティを適用し、他のコードがセマフォを制御できないようにします。
nameには、名前空間を指定するために、Global\またはLocal\のプレフィックスを付ける場合があります。
Global名前空間を指定すると、同期オブジェクトをシステム上の任意のプロセスと共有できます。
Local名前空間が指定されている場合(名前空間が指定されていない場合の既定値)、同期オブジェクトを同じセッション内のプロセスと共有できます。 Windowsでは、セッションはログイン セッションであり、サービスは通常、別の非対話型セッションで実行されます。 Unix に似たオペレーティング システムでは、各シェルに独自のセッションがあります。 セッションとローカルの同期オブジェクトは、プロセス間の同期に適している場合があります。このオブジェクトはすべて同じセッションで実行されます。 Windowsの同期オブジェクト名の詳細については、「Object Names」を参照してください。
nameが指定されていて、要求された型の同期オブジェクトが名前空間に既に存在する場合は、既存の同期オブジェクトが使用されます。 別の型の同期オブジェクトが既に名前空間に存在する場合は、 WaitHandleCannotBeOpenedException がスローされます。 それ以外の場合は、新しい同期オブジェクトが作成されます。
このコンストラクターは、名前付きシステム セマフォを表す Semaphore オブジェクトを初期化します。 同じ名前付きシステム セマフォを表す複数の Semaphore オブジェクトを作成できます。
名前付きシステム セマフォが存在しない場合は、指定されたアクセス制御セキュリティを使用して作成されます。 名前付きセマフォが存在する場合、指定されたアクセス制御セキュリティは無視されます。
注
呼び出し元は、semaphoreSecurityが現在のユーザーにアクセス権許可しない場合でも、新しく作成されたSemaphore オブジェクトを完全に制御できます。 ただし、現在のユーザーが、コンストラクターまたは OpenExisting メソッドを使用して、同じ名前付きセマフォを表す別の Semaphore オブジェクトを取得しようとすると、Windowsアクセス制御セキュリティが適用されます。
名前付きシステム セマフォが存在しない場合は、 initialCount および maximumCountで指定された初期カウントと最大カウントで作成されます。 名前付きシステム セマフォが既に存在する場合、 initialCount と maximumCount は使用されませんが、無効な値を指定しても例外が発生します。
createdNew パラメーターを使用して、システム セマフォがこのコンストラクターによって作成されたかどうかを判断します。
initialCountがmaximumCount未満で、createdNewがtrue場合、現在のスレッドがWaitOne (maximumCount - initialCount) 回呼び出した場合と同じ効果が得られます。
nameにnullまたは空の文字列を指定すると、Semaphore(Int32, Int32) コンストラクターオーバーロードを呼び出したかのようにローカル セマフォが作成されます。 この場合、 createdNew は常に true。
名前付きセマフォはオペレーティング システム全体で表示されるため、プロセス境界を越えてリソースの使用を調整するために使用できます。
Caution
既定では、名前付きセマフォは、それを作成したユーザーに制限されません。 セマフォを複数回取得して解放しないことでセマフォを妨害するなど、他のユーザーがセマフォを開いて使用できる場合があります。 特定のユーザーへのアクセスを制限するために、名前付きセマフォの作成時に SemaphoreSecurity を渡すことができます。 信頼されていないユーザーがコードを実行している可能性があるシステムでは、アクセス制限なしで名前付きセマフォを使用しないでください。
こちらもご覧ください
適用対象
Semaphore(Int32, Int32, String, NamedWaitHandleOptions, Boolean)
- ソース:
- Semaphore.cs
- ソース:
- Semaphore.cs
Semaphore クラスの新しいインスタンスを初期化します。エントリの初期数と同時エントリの最大数を指定し、必要に応じてシステム セマフォ オブジェクトの名前と、ユーザー スコープとセッション スコープのアクセスを設定するオプションを指定し、新しいシステム セマフォが作成されたかどうかを示す値を受け取る変数を指定します。
public:
Semaphore(int initialCount, int maximumCount, System::String ^ name, System::Threading::NamedWaitHandleOptions options, [Runtime::InteropServices::Out] bool % createdNew);
public Semaphore(int initialCount, int maximumCount, string? name, System.Threading.NamedWaitHandleOptions options, out bool createdNew);
new System.Threading.Semaphore : int * int * string * System.Threading.NamedWaitHandleOptions * bool -> System.Threading.Semaphore
Public Sub New (initialCount As Integer, maximumCount As Integer, name As String, options As NamedWaitHandleOptions, ByRef createdNew As Boolean)
パラメーター
- initialCount
- Int32
同時に満たすことができるセマフォの要求の初期数。
- maximumCount
- Int32
同時に満たすことができるセマフォの要求の最大数。
- name
- String
同期オブジェクトを他のプロセスと共有する場合の名前。それ以外の場合は、 null または空の文字列。 名前では大文字と小文字が区別されます。
- options
- NamedWaitHandleOptions
名前付きセマフォのスコープ オプション。 既定では、アクセスは現在のユーザーと現在のセッションのみに制限されます。 指定したオプションは、名前の名前空間と、基になるセマフォ オブジェクトへのアクセスに影響する可能性があります。
- createdNew
- Boolean
このメソッドが戻るときに、ローカル セマフォが作成された場合 (つまり、nameがnullまたは空の文字列の場合) または指定した名前付きシステム セマフォが作成された場合、trueが含まれます。指定した名前付きシステム セマフォが既に存在する場合は、falseが含まれます。 このパラメーターは初期化せずに渡されます。
例外
name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。
-又は-
その他のエラーが発生しました。
HResultプロパティは、詳細情報を提供する場合があります。
Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。
name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。
名前付きセマフォが存在し、アクセス制御セキュリティを持ち、ユーザーに FullControlがありません。
指定された name を持つ同期オブジェクトを作成できません。 異なる種類の同期オブジェクトの名前が同じである可能性があります。
-又は-
指定した name を持つオブジェクトが存在しますが、指定した options は既存のオブジェクトのオプションと互換性がありません。
注釈
nameが指定され、要求された型の同期オブジェクトが名前空間に既に存在する場合、optionsが現在のユーザーに制限されたアクセスを指定し、同期オブジェクトと互換性がない場合を除き、既存の同期オブジェクトが使用されます。この場合、WaitHandleCannotBeOpenedExceptionがスローされます。 別の型の同期オブジェクトが既に名前空間に存在する場合は、 WaitHandleCannotBeOpenedException もスローされます。 それ以外の場合は、新しい同期オブジェクトが作成されます。
このコンストラクターは、名前付きシステム セマフォを表す Semaphore オブジェクトを初期化します。 同じ名前付きシステム セマフォを表す複数の Semaphore オブジェクトを作成できます。
名前付きシステム セマフォが存在しない場合は、 initialCount および maximumCountで指定された初期カウントと最大カウントで作成されます。 名前付きシステム セマフォが既に存在する場合、 initialCount と maximumCount は使用されませんが、無効な値を指定しても例外が発生します。
createdNew パラメーターを使用して、システム セマフォがこのコンストラクターによって作成されたかどうかを判断します。
initialCountがmaximumCount未満で、createdNewがtrue場合、現在のスレッドがWaitOne (maximumCount - initialCount) 回呼び出した場合と同じ効果が得られます。
nameにnullまたは空の文字列を指定すると、Semaphore(Int32, Int32) コンストラクターオーバーロードを呼び出したかのようにローカル セマフォが作成されます。 この場合、 createdNew は常に true。
名前付きセマフォはオペレーティング システム全体で表示されるため、プロセス境界を越えてリソースの使用を調整するために使用できます。
Windowsでは、optionsを指定して、名前付きセマフォに現在のユーザーのみがアクセスできるか、すべてのユーザーにアクセスできるかを指定できます。 また、名前付きセマフォに現在のセッション内のプロセスのみにアクセスするか、すべてのセッションにアクセスできるかを指定することもできます。 詳細については、NamedWaitHandleOptionsを参照してください。
Caution
Unix ベースのオペレーティング システムでは、名前付きセマフォがサポートされていないため、 options パラメーターは無効です。