public override void Close()
        {
            if (connectionClosed != null)
            {
                bool isClosed = connectionClosed.WaitOne(0);
                if (!isClosed)
                {
                    connectionClosed.Set();
                    connectionClosed.Close();
                }
                connectionClosed = null;
                EventWaitHandle[] handles = { serverRead, serverWrote, clientRead, clientWrote };

                for (int i = 0; i < handles.Length; i++)
                {
                    if (handles[i] != null)
                    {
                        handles[i].Close();
                    }
                }
                if (data != null)
                {
                    data.Dispose();
                    data = null;
                }
            }
        }
        private void SetupEvents()
        {
            string prefix = memoryName + "_" + connectNumber;

            data             = new SharedMemory(prefix + "_DATA", (IntPtr)BUFFERLENGTH);
            serverWrote      = EventWaitHandle.OpenExisting(prefix + "_SERVER_WROTE");
            serverRead       = EventWaitHandle.OpenExisting(prefix + "_SERVER_READ");
            clientWrote      = EventWaitHandle.OpenExisting(prefix + "_CLIENT_WROTE");
            clientRead       = EventWaitHandle.OpenExisting(prefix + "_CLIENT_READ");
            connectionClosed = EventWaitHandle.OpenExisting(prefix + "_CONNECTION_CLOSED");

            // tell the server we are ready
            serverRead.Set();
        }
        private void GetConnectNumber(uint timeOut)
        {
            EventWaitHandle connectRequest;

            try
            {
                connectRequest =
                    EventWaitHandle.OpenExisting(memoryName + "_CONNECT_REQUEST");
            }
            catch (Exception)
            {
                // If server runs as service, its shared memory is global
                // And if connector runs in user session, it needs to prefix
                // shared memory name with "Global\"
                string prefixedMemoryName = @"Global\" + memoryName;
                connectRequest =
                    EventWaitHandle.OpenExisting(prefixedMemoryName + "_CONNECT_REQUEST");
                memoryName = prefixedMemoryName;
            }
            EventWaitHandle connectAnswer =
                EventWaitHandle.OpenExisting(memoryName + "_CONNECT_ANSWER");

            using (SharedMemory connectData =
                       new SharedMemory(memoryName + "_CONNECT_DATA", (IntPtr)4))
            {
                // now start the connection
                if (!connectRequest.Set())
                {
                    throw new MySqlException("Failed to open shared memory connection");
                }
                if (!connectAnswer.WaitOne((int)(timeOut * 1000), false))
                {
                    throw new MySqlException("Timeout during connection");
                }
                connectNumber = Marshal.ReadInt32(connectData.View);
            }
        }
    private void SetupEvents()
    {
      string prefix = memoryName + "_" + connectNumber;
      data = new SharedMemory(prefix + "_DATA", (IntPtr)BUFFERLENGTH);
      serverWrote = EventWaitHandle.OpenExisting(prefix + "_SERVER_WROTE");
      serverRead = EventWaitHandle.OpenExisting(prefix + "_SERVER_READ");
      clientWrote = EventWaitHandle.OpenExisting(prefix + "_CLIENT_WROTE");
      clientRead = EventWaitHandle.OpenExisting(prefix + "_CLIENT_READ");
      connectionClosed = EventWaitHandle.OpenExisting(prefix + "_CONNECTION_CLOSED");

      // tell the server we are ready
      serverRead.Set();
    }
    private void GetConnectNumber(uint timeOut)
    {
      EventWaitHandle connectRequest;
      try
      {
        connectRequest =
            EventWaitHandle.OpenExisting(memoryName + "_CONNECT_REQUEST");

      }
      catch (Exception)
      {
        // If server runs as service, its shared memory is global 
        // And if connector runs in user session, it needs to prefix
        // shared memory name with "Global\"
        string prefixedMemoryName = @"Global\" + memoryName;
        connectRequest =
            EventWaitHandle.OpenExisting(prefixedMemoryName + "_CONNECT_REQUEST");
        memoryName = prefixedMemoryName;
      }
      EventWaitHandle connectAnswer =
         EventWaitHandle.OpenExisting(memoryName + "_CONNECT_ANSWER");
      using (SharedMemory connectData =
          new SharedMemory(memoryName + "_CONNECT_DATA", (IntPtr)4))
      {
        // now start the connection
        if (!connectRequest.Set())
          throw new MySqlException("Failed to open shared memory connection");
        if (!connectAnswer.WaitOne((int)(timeOut * 1000), false))
          throw new MySqlException("Timeout during connection");
        connectNumber = Marshal.ReadInt32(connectData.View);
      }
    }
    public override void Close()
    {
      if (connectionClosed != null)
      {
        bool isClosed = connectionClosed.WaitOne(0);
        if (!isClosed)
        {
          connectionClosed.Set();
          connectionClosed.Close();
        }
        connectionClosed = null;
        EventWaitHandle[] handles = { serverRead, serverWrote, clientRead, clientWrote };

        for (int i = 0; i < handles.Length; i++)
        {
          if (handles[i] != null)
            handles[i].Close();
        }
        if (data != null)
        {
          data.Dispose();
          data = null;
        }
      }
    }