EventWaitHandle.OpenExisting メソッド

定義

指定した名前付き同期イベントが既に存在する場合は開きます。

オーバーロード

名前 説明
OpenExisting(String)

指定した名前付き同期イベントが既に存在する場合は開きます。

OpenExisting(String, EventWaitHandleRights)

指定した名前付き同期イベントが既に存在する場合は、目的のセキュリティ アクセスを使用して開きます。

OpenExisting(String, NamedWaitHandleOptions)

指定した名前付き同期イベントが既に存在する場合は開きます。 オプションが現在のユーザーのみに設定されている場合、オブジェクトのアクセス制御は呼び出し元のユーザーに対して検証されます。

OpenExisting(String)

ソース:
EventWaitHandle.cs
ソース:
EventWaitHandle.cs
ソース:
EventWaitHandle.cs
ソース:
EventWaitHandle.cs
ソース:
EventWaitHandle.cs

指定した名前付き同期イベントが既に存在する場合は開きます。

public:
 static System::Threading::EventWaitHandle ^ OpenExisting(System::String ^ name);
[System.Security.SecurityCritical]
public static System.Threading.EventWaitHandle OpenExisting(string name);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Threading.EventWaitHandle OpenExisting(string name);
public static System.Threading.EventWaitHandle OpenExisting(string name);
[<System.Security.SecurityCritical>]
static member OpenExisting : string -> System.Threading.EventWaitHandle
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member OpenExisting : string -> System.Threading.EventWaitHandle
static member OpenExisting : string -> System.Threading.EventWaitHandle
Public Shared Function OpenExisting (name As String) As EventWaitHandle

パラメーター

name
String

開いて他のプロセスと共有する同期オブジェクトの名前。 名前では大文字と小文字が区別されます。 円記号 (\) は予約されており、名前空間の指定にのみ使用できます。 名前空間の詳細については、「解説」セクションを参照してください。 オペレーティング システムによっては、名前にさらに制限がある場合があります。 たとえば、Unix ベースのオペレーティング システムでは、名前空間を除外した後の名前は有効なファイル名である必要があります。

返品

名前付きシステム イベントを表すオブジェクト。

属性

例外

指定された name を持つ同期オブジェクトを開くことができません。 存在しないか、異なる型の同期オブジェクトの名前が同じである可能性があります。 場合によっては、無効な名前に対してこの例外がスローされることがあります。

name は空の文字列です。

-又は-

.NET Framework のみ: name がMAX_PATH (260 文字) より長くなっています。

namenullです。

name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。

-又は-

その他のエラーが発生しました。 HResultプロパティは、詳細情報を提供する場合があります。

Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。

name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。

名前付きイベントは存在しますが、ユーザーはそれを使用するために必要なセキュリティ アクセス権を持っていません。

注釈

nameの先頭に Global\ またはLocal\を付けて名前空間を指定できます。 Global名前空間を指定すると、同期オブジェクトをシステム上の任意のプロセスと共有できます。 Local名前空間が指定されている場合(名前空間が指定されていない場合も既定値)、同期オブジェクトを同じセッション内のプロセスと共有できます。 Windowsでは、セッションはログイン セッションであり、サービスは通常、別の非対話型セッションで実行されます。 Unix に似たオペレーティング システムでは、各シェルに独自のセッションがあります。 セッションとローカルの同期オブジェクトは、プロセス間の同期に適している場合があります。このオブジェクトはすべて同じセッションで実行されます。 Windowsの同期オブジェクト名の詳細については、「Object Names」を参照してください。

要求された型の同期オブジェクトが名前空間に存在する場合は、既存の同期オブジェクトが開かれます。 同期オブジェクトが名前空間に存在しない場合、または別の型の同期オブジェクトが名前空間に存在する場合は、 WaitHandleCannotBeOpenedException がスローされます。

OpenExisting メソッドは、指定した名前付きシステム イベントを開こうとします。 システム イベントがまだ存在しない場合に作成するには、EventWaitHandle パラメーターを持つname コンストラクターのいずれかを使用します。

nameに同じ値を使用するこのメソッドを複数回呼び出しても、返されるオブジェクトが同じ名前付きシステム イベントを表していても、必ずしも同じEventWaitHandle オブジェクトを返すわけではありません。

.NET Framework では、このメソッド のオーバーロードは、System.Threading.EventWaitHandle.OpenExisting(System.String,System.Security.AccessControl.EventWaitHandleRights) メソッド オーバーロードを呼び出し、EventWaitHandleRights.Synchronize および EventWaitHandleRights.Modify 権限を指定することと同じです。 ビットごとのOR演算を使用して結合されます。

EventWaitHandleRights.Synchronize フラグを指定すると、スレッドは名前付きシステム イベントを待機でき、EventWaitHandleRights.Modify フラグを指定すると、スレッドはSetメソッドとResetメソッドを呼び出すことができます。

こちらもご覧ください

適用対象

OpenExisting(String, EventWaitHandleRights)

指定した名前付き同期イベントが既に存在する場合は、目的のセキュリティ アクセスを使用して開きます。

public:
 static System::Threading::EventWaitHandle ^ OpenExisting(System::String ^ name, System::Security::AccessControl::EventWaitHandleRights rights);
public static System.Threading.EventWaitHandle OpenExisting(string name, System.Security.AccessControl.EventWaitHandleRights rights);
[System.Security.SecurityCritical]
public static System.Threading.EventWaitHandle OpenExisting(string name, System.Security.AccessControl.EventWaitHandleRights rights);
static member OpenExisting : string * System.Security.AccessControl.EventWaitHandleRights -> System.Threading.EventWaitHandle
[<System.Security.SecurityCritical>]
static member OpenExisting : string * System.Security.AccessControl.EventWaitHandleRights -> System.Threading.EventWaitHandle
Public Shared Function OpenExisting (name As String, rights As EventWaitHandleRights) As EventWaitHandle

パラメーター

name
String

開いて他のプロセスと共有する同期オブジェクトの名前。 名前では大文字と小文字が区別されます。 円記号 (\) は予約されており、名前空間の指定にのみ使用できます。 名前空間の詳細については、「解説」セクションを参照してください。 オペレーティング システムによっては、名前にさらに制限がある場合があります。 たとえば、Unix ベースのオペレーティング システムでは、名前空間を除外した後の名前は有効なファイル名である必要があります。

rights
EventWaitHandleRights

目的のセキュリティ アクセスを表す列挙値のビットごとの組み合わせ。

返品

名前付きシステム イベントを表すオブジェクト。

属性

例外

name は空の文字列です。

-又は-

.NET Framework のみ: name がMAX_PATH (260 文字) より長くなっています。

namenullです。

指定された name を持つ同期オブジェクトを開くことができません。 存在しないか、異なる型の同期オブジェクトの名前が同じである可能性があります。 場合によっては、無効な名前に対してこの例外がスローされることがあります。

name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。

-又は-

その他のエラーが発生しました。 HResultプロパティは、詳細情報を提供する場合があります。

Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。

name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。

名前付きイベントは存在しますが、ユーザーは必要なセキュリティ アクセス権を持っていません。

次のコード例は、アクセス制御セキュリティを持つ名前付きシステム イベントのクロスプロセス動作を示しています。 この例では、 OpenExisting(String) メソッドのオーバーロードを使用して、名前付きイベントの存在をテストします。

イベントが存在しない場合は、イベントを使用する権限を現在のユーザーに拒否する最初の所有権とアクセス制御セキュリティを使用して作成されますが、イベントの読み取りと変更のアクセス許可を付与します。

2 つのコマンド ウィンドウからコンパイルされた例を実行すると、2 番目のコピーは、 OpenExisting(String)の呼び出しでアクセス違反の例外をスローします。 例外がキャッチされ、この例では、 OpenExisting(String, EventWaitHandleRights) メソッドのオーバーロードを使用して、アクセス許可の読み取りと変更に必要な権限を持つイベントを待機します。

アクセス許可が変更されると、イベントは待機して通知するために必要な権限で開かれます。 3 番目のコマンド ウィンドウからコンパイルされた例を実行した場合、この例は新しいアクセス許可を使用して実行されます。

using System;
using System.Threading;
using System.Security.AccessControl;

internal class Example
{
    internal static void Main()
    {
        const string ewhName = "EventWaitHandleExample5";

        EventWaitHandle ewh = null;
        bool doesNotExist = false;
        bool unauthorized = false;

        // The value of this variable is set by the event
        // constructor. It is true if the named system event was
        // created, and false if the named event already existed.
        //
        bool wasCreated;

        // Attempt to open the named event.
        try
        {
            // Open the event with (EventWaitHandleRights.Synchronize
            // | EventWaitHandleRights.Modify), to wait on and 
            // signal the named event.
            //
            ewh = EventWaitHandle.OpenExisting(ewhName);
        }
        catch (WaitHandleCannotBeOpenedException)
        {
            Console.WriteLine("Named event does not exist.");
            doesNotExist = true;
        }
        catch (UnauthorizedAccessException ex)
        {
            Console.WriteLine("Unauthorized access: {0}", ex.Message);
            unauthorized = true;
        }

        // There are three cases: (1) The event does not exist.
        // (2) The event exists, but the current user doesn't 
        // have access. (3) The event exists and the user has
        // access.
        //
        if (doesNotExist)
        {
            // The event does not exist, so create it.

            // Create an access control list (ACL) that denies the
            // current user the right to wait on or signal the 
            // event, but allows the right to read and change
            // security information for the event.
            //
            string user = Environment.UserDomainName + "\\"
                + Environment.UserName;
            EventWaitHandleSecurity ewhSec = 
                new EventWaitHandleSecurity();

            EventWaitHandleAccessRule rule = 
                new EventWaitHandleAccessRule(user, 
                    EventWaitHandleRights.Synchronize | 
                    EventWaitHandleRights.Modify, 
                    AccessControlType.Deny);
            ewhSec.AddAccessRule(rule);

            rule = new EventWaitHandleAccessRule(user, 
                EventWaitHandleRights.ReadPermissions | 
                EventWaitHandleRights.ChangePermissions, 
                AccessControlType.Allow);
            ewhSec.AddAccessRule(rule);

            // Create an EventWaitHandle object that represents
            // the system event named by the constant 'ewhName', 
            // initially signaled, with automatic reset, and with
            // the specified security access. The Boolean value that 
            // indicates creation of the underlying system object
            // is placed in wasCreated.
            //
            ewh = new EventWaitHandle(true, 
                EventResetMode.AutoReset, 
                ewhName, 
                out wasCreated, 
                ewhSec);

            // If the named system event was created, it can be
            // used by the current instance of this program, even 
            // though the current user is denied access. The current
            // program owns the event. Otherwise, exit the program.
            // 
            if (wasCreated)
            {
                Console.WriteLine("Created the named event.");
            }
            else
            {
                Console.WriteLine("Unable to create the event.");
                return;
            }
        }
        else if (unauthorized)
        {
            // Open the event to read and change the access control
            // security. The access control security defined above
            // allows the current user to do this.
            //
            try
            {
                ewh = EventWaitHandle.OpenExisting(ewhName, 
                    EventWaitHandleRights.ReadPermissions | 
                    EventWaitHandleRights.ChangePermissions);

                // Get the current ACL. This requires 
                // EventWaitHandleRights.ReadPermissions.
                EventWaitHandleSecurity ewhSec = ewh.GetAccessControl();
                
                string user = Environment.UserDomainName + "\\"
                    + Environment.UserName;

                // First, the rule that denied the current user 
                // the right to enter and release the event must
                // be removed.
                EventWaitHandleAccessRule rule = 
                    new EventWaitHandleAccessRule(user, 
                        EventWaitHandleRights.Synchronize | 
                        EventWaitHandleRights.Modify, 
                        AccessControlType.Deny);
                ewhSec.RemoveAccessRule(rule);

                // Now grant the user the correct rights.
                // 
                rule = new EventWaitHandleAccessRule(user, 
                    EventWaitHandleRights.Synchronize | 
                    EventWaitHandleRights.Modify, 
                    AccessControlType.Allow);
                ewhSec.AddAccessRule(rule);

                // Update the ACL. This requires
                // EventWaitHandleRights.ChangePermissions.
                ewh.SetAccessControl(ewhSec);

                Console.WriteLine("Updated event security.");

                // Open the event with (EventWaitHandleRights.Synchronize 
                // | EventWaitHandleRights.Modify), the rights required
                // to wait on and signal the event.
                //
                ewh = EventWaitHandle.OpenExisting(ewhName);
            }
            catch (UnauthorizedAccessException ex)
            {
                Console.WriteLine("Unable to change permissions: {0}",
                    ex.Message);
                return;
            }
        }

        // Wait on the event, and hold it until the program
        // exits.
        //
        try
        {
            Console.WriteLine("Wait on the event.");
            ewh.WaitOne();
            Console.WriteLine("Event was signaled.");
            Console.WriteLine("Press the Enter key to signal the event and exit.");
            Console.ReadLine();
        }
        catch (UnauthorizedAccessException ex)
        {
            Console.WriteLine("Unauthorized access: {0}", ex.Message);
        }
        finally
        {
            ewh.Set();
        }
    }
}
Imports System.Threading
Imports System.Security.AccessControl

Friend Class Example

    <MTAThread> _
    Friend Shared Sub Main()
        Const ewhName As String = "EventWaitHandleExample5"

        Dim ewh As EventWaitHandle = Nothing
        Dim doesNotExist as Boolean = False
        Dim unauthorized As Boolean = False

        ' The value of this variable is set by the event
        ' constructor. It is True if the named system event was
        ' created, and False if the named event already existed.
        '
        Dim wasCreated As Boolean

        ' Attempt to open the named event.
        Try
            ' Open the event with (EventWaitHandleRights.Synchronize
            ' Or EventWaitHandleRights.Modify), to wait on and 
            ' signal the named event.
            '
            ewh = EventWaitHandle.OpenExisting(ewhName)
        Catch ex As WaitHandleCannotBeOpenedException
            Console.WriteLine("Named event 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 event does not exist.
        ' (2) The event exists, but the current user doesn't 
        ' have access. (3) The event exists and the user has
        ' access.
        '
        If doesNotExist Then
            ' The event does not exist, so create it.

            ' Create an access control list (ACL) that denies the
            ' current user the right to wait on or signal the 
            ' event, but allows the right to read and change
            ' security information for the event.
            '
            Dim user As String = Environment.UserDomainName _ 
                & "\" & Environment.UserName
            Dim ewhSec As New EventWaitHandleSecurity()

            Dim rule As New EventWaitHandleAccessRule(user, _
                EventWaitHandleRights.Synchronize Or _
                EventWaitHandleRights.Modify, _
                AccessControlType.Deny)
            ewhSec.AddAccessRule(rule)

            rule = New EventWaitHandleAccessRule(user, _
                EventWaitHandleRights.ReadPermissions Or _
                EventWaitHandleRights.ChangePermissions, _
                AccessControlType.Allow)
            ewhSec.AddAccessRule(rule)

            ' Create an EventWaitHandle object that represents
            ' the system event named by the constant 'ewhName', 
            ' initially signaled, with automatic reset, and with
            ' the specified security access. The Boolean value that 
            ' indicates creation of the underlying system object
            ' is placed in wasCreated.
            '
            ewh = New EventWaitHandle(True, _
                EventResetMode.AutoReset, ewhName, _
                wasCreated, ewhSec)

            ' If the named system event was created, it can be
            ' used by the current instance of this program, even 
            ' though the current user is denied access. The current
            ' program owns the event. Otherwise, exit the program.
            ' 
            If wasCreated Then
                Console.WriteLine("Created the named event.")
            Else
                Console.WriteLine("Unable to create the event.")
                Return
            End If

        ElseIf unauthorized Then

            ' Open the event to read and change the access control
            ' security. The access control security defined above
            ' allows the current user to do this.
            '
            Try
                ewh = EventWaitHandle.OpenExisting(ewhName, _
                    EventWaitHandleRights.ReadPermissions Or _
                    EventWaitHandleRights.ChangePermissions)

                ' Get the current ACL. This requires 
                ' EventWaitHandleRights.ReadPermissions.
                Dim ewhSec As EventWaitHandleSecurity = _
                    ewh.GetAccessControl()
                
                Dim user As String = Environment.UserDomainName _ 
                    & "\" & Environment.UserName

                ' First, the rule that denied the current user 
                ' the right to enter and release the event must
                ' be removed.
                Dim rule As New EventWaitHandleAccessRule(user, _
                    EventWaitHandleRights.Synchronize Or _
                    EventWaitHandleRights.Modify, _
                    AccessControlType.Deny)
                ewhSec.RemoveAccessRule(rule)

                ' Now grant the user the correct rights.
                ' 
                rule = New EventWaitHandleAccessRule(user, _
                    EventWaitHandleRights.Synchronize Or _
                    EventWaitHandleRights.Modify, _
                    AccessControlType.Allow)
                ewhSec.AddAccessRule(rule)

                ' Update the ACL. This requires
                ' EventWaitHandleRights.ChangePermissions.
                ewh.SetAccessControl(ewhSec)

                Console.WriteLine("Updated event security.")

                ' Open the event with (EventWaitHandleRights.Synchronize 
                ' Or EventWaitHandleRights.Modify), the rights required
                ' to wait on and signal the event.
                '
                ewh = EventWaitHandle.OpenExisting(ewhName)

            Catch ex As UnauthorizedAccessException
                Console.WriteLine("Unable to change permissions: {0}", _
                    ex.Message)
                Return
            End Try

        End If

        ' Wait on the event, and hold it until the program
        ' exits.
        '
        Try
            Console.WriteLine("Wait on the event.")
            ewh.WaitOne()
            Console.WriteLine("Event was signaled.")
            Console.WriteLine("Press the Enter key to signal the event and exit.")
            Console.ReadLine()
        Catch ex As UnauthorizedAccessException
            Console.WriteLine("Unauthorized access: {0}", _
                ex.Message)
        Finally
            ewh.Set()
        End Try
    End Sub 
End Class

注釈

nameの先頭に Global\ またはLocal\を付けて名前空間を指定できます。 Global名前空間を指定すると、同期オブジェクトをシステム上の任意のプロセスと共有できます。 Local名前空間が指定されている場合(名前空間が指定されていない場合も既定値)、同期オブジェクトを同じセッション内のプロセスと共有できます。 Windowsでは、セッションはログイン セッションであり、サービスは通常、別の非対話型セッションで実行されます。 Unix に似たオペレーティング システムでは、各シェルに独自のセッションがあります。 セッションとローカルの同期オブジェクトは、プロセス間の同期に適している場合があります。このオブジェクトはすべて同じセッションで実行されます。 Windowsの同期オブジェクト名の詳細については、「Object Names」を参照してください。

要求された型の同期オブジェクトが名前空間に存在する場合は、既存の同期オブジェクトが開かれます。 同期オブジェクトが名前空間に存在しない場合、または別の型の同期オブジェクトが名前空間に存在する場合は、 WaitHandleCannotBeOpenedException がスローされます。

rights パラメーターには、スレッドがイベントを待機できるようにするための EventWaitHandleRights.Synchronize フラグと、スレッドが EventWaitHandleRights.Modify メソッドと Set メソッドを呼び出すことができるようにするReset フラグを含める必要があります。

OpenExisting メソッドは、既存の名前付きシステム イベントを開こうとします。 システム イベントがまだ存在しない場合に作成するには、EventWaitHandle パラメーターを持つname コンストラクターのいずれかを使用します。

nameに同じ値を使用するこのメソッドを複数回呼び出しても、返されるオブジェクトが同じ名前付きシステム イベントを表していても、必ずしも同じEventWaitHandle オブジェクトを返すわけではありません。

こちらもご覧ください

適用対象

OpenExisting(String, NamedWaitHandleOptions)

ソース:
EventWaitHandle.cs
ソース:
EventWaitHandle.cs

指定した名前付き同期イベントが既に存在する場合は開きます。 オプションが現在のユーザーのみに設定されている場合、オブジェクトのアクセス制御は呼び出し元のユーザーに対して検証されます。

public:
 static System::Threading::EventWaitHandle ^ OpenExisting(System::String ^ name, System::Threading::NamedWaitHandleOptions options);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Threading.EventWaitHandle OpenExisting(string name, System.Threading.NamedWaitHandleOptions options);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member OpenExisting : string * System.Threading.NamedWaitHandleOptions -> System.Threading.EventWaitHandle
Public Shared Function OpenExisting (name As String, options As NamedWaitHandleOptions) As EventWaitHandle

パラメーター

name
String

開いて他のプロセスと共有する同期オブジェクトの名前。 名前では大文字と小文字が区別されます。

options
NamedWaitHandleOptions

名前付きハンドルのスコープ オプション。 既定では、アクセスは現在のユーザーと現在のセッションのみに制限されます。 指定したオプションは、名前の名前空間と、基になるハンドル オブジェクトへのアクセスに影響する可能性があります。

返品

名前付きシステム イベントを表すオブジェクト。

属性

例外

指定された name を持つ同期オブジェクトを開くことができません。 存在しないか、異なる型の同期オブジェクトの名前が同じである可能性があります。 場合によっては、無効な名前に対してこの例外がスローされることがあります。

-又は-

指定した name を持つオブジェクトが存在しますが、指定した options は既存のオブジェクトのオプションと互換性がありません。

name は空の文字列です。

namenullです。

name が無効です。 これは、不明なプレフィックスや無効な文字など、オペレーティング システムによって適用される可能性のあるいくつかの制限など、さまざまな理由で発生する可能性があります。 名前と共通プレフィックス "Global\" と "Local\" では大文字と小文字が区別されることに注意してください。

-又は-

その他のエラーが発生しました。 HResultプロパティは、詳細情報を提供する場合があります。

Windowsのみ: nameが不明な名前空間を指定しました。 詳細については、「 オブジェクト名」 を参照してください。

name は長すぎます。 長さの制限は、オペレーティング システムまたは構成によって異なります。

名前付きイベントは存在しますが、ユーザーはそれを使用するために必要なセキュリティ アクセス権を持っていません。

注釈

要求された型の同期オブジェクトが名前空間に存在する場合は、既存の同期オブジェクトが開かれます。 ただし、 options が現在のユーザーに限定されたアクセスを指定し、同期オブジェクトと互換性がない場合は、 WaitHandleCannotBeOpenedException がスローされます。 同期オブジェクトが名前空間に存在しない場合、または別の型の同期オブジェクトが名前空間に存在する場合は、 WaitHandleCannotBeOpenedException もスローされます。

OpenExisting メソッドは、指定した名前付きシステム イベントを開こうとします。 システム イベントがまだ存在しない場合に作成するには、EventWaitHandle パラメーターを持つname コンストラクターのいずれかを使用します。

nameに同じ値を使用するこのメソッドを複数回呼び出しても、返されるオブジェクトが同じ名前付きシステム イベントを表していても、必ずしも同じEventWaitHandle オブジェクトを返すわけではありません。

こちらもご覧ください

適用対象