コード例 #1
0
        private NamedPipeClient <MessageContainer> TryOpenPipe()
        {
            NamedPipeClient <MessageContainer> client = null;

            try
            {
                client = new NamedPipeClient <MessageContainer>(_pipeName);
                client.ServerMessage += OnServerMessage;
                client.Error         += OnError;
                client.Start();
            }
            catch
            {
                //TODO
            }
            return(client);
        }
コード例 #2
0
        /// <summary>
        /// Closes all open client connections and stops listening for new ones.
        /// </summary>
        public void Stop()
        {
            _shouldKeepRunning = false;

            lock (_connections)
            {
                foreach (var client in _connections.ToArray())
                {
                    client.Close();
                }
            }

            // If background thread is still listening for a client to connect,
            // initiate a dummy connection that will allow the thread to exit.
            var dummyClient = new NamedPipeClient <TRead, TWrite>(_pipeName);

            dummyClient.Start();
            dummyClient.WaitForConnection(TimeSpan.FromSeconds(2));
            dummyClient.Stop();
            dummyClient.WaitForDisconnection(TimeSpan.FromSeconds(2));
        }