Export (0) Print
Expand All

EventWaitHandle.OpenExisting Method (String)

Opens the specified named synchronization event, if it already exists.

Namespace:  System.Threading
Assemblies:   System.Threading (in System.Threading.dll)
  mscorlib (in mscorlib.dll)

public static EventWaitHandle OpenExisting(
	string name
)

Parameters

name
Type: System.String

The name of the system synchronization event to open.

Return Value

Type: System.Threading.EventWaitHandle
An object that represents the named system event.

ExceptionCondition
ArgumentException

name is an empty string.

-or-

name is longer than 260 characters.

ArgumentNullException

name is null.

WaitHandleCannotBeOpenedException

The named system event does not exist.

IOException

A Win32 error occurred.

UnauthorizedAccessException

The named event exists, but the user does not have the security access required to use it.

The OpenExisting method tries to open the specified named system event. If the system event does not exist, this method throws an exception instead of creating the system event. To create the system event when it does not already exist, use one of the EventWaitHandle constructors that has a name parameter.

Multiple calls to this method that use the same value for name do not necessarily return the same EventWaitHandle object, even though the objects that are returned represent the same named system event.

This method overload is equivalent to calling the OpenExisting(String, EventWaitHandleRights) method overload and specifying EventWaitHandleRights.Synchronize and EventWaitHandleRights.Modify rights, combined by using the bitwise OR operation.

Specifying the EventWaitHandleRights.Synchronize flag allows a thread to wait on the named system event, and specifying the EventWaitHandleRights.Modify flag allows a thread to call the Set and Reset methods.

The following code example demonstrates the cross-process behavior of a named system event with access control security. The example uses the OpenExisting(String) method overload to test for the existence of a named event.

If the event does not exist, it is created with initial ownership and access control security that denies the current user the right to use the event, but grants the right to read and change permissions on the event.

If you run the compiled example from two command windows, the second copy will throw an access violation exception on the call to OpenExisting(String). The exception is caught, and the example uses the OpenExisting(String, EventWaitHandleRights) method overload to wait on the event with the rights needed to read and change the permissions.

After the permissions are changed, the event is opened with the rights required to wait on it and signal it. If you run the compiled example from a third command window, the example runs using the new permissions.

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();
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

  • SecurityCriticalAttribute 

    Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft