public void StartServer() { Console.WriteLine("[PIPE SERVER] Starting pipe server."); new Thread(delegate() { var ps = new PipeSecurity(); ps.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow)); while (true) { var pipeServer = NamedPipeServerStreamConstructors.New("ShotrInputHandler", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.WriteThrough, 1024, 1024, ps); try { Console.WriteLine("[PIPE SERVER] Waiting for client..."); pipeServer.WaitForConnection(); Console.WriteLine("[PIPE SERVER] Got client through pipe server."); try { var ss = new StreamString(pipeServer); var data = ss.ReadString(); Console.WriteLine($"[PIPE SERVER] Received the message: {data}."); PipeServerReceivedClient.Invoke(this, new PipeServerEventArgs(data)); } catch (IOException e) { Console.WriteLine("[PIPE SERVER] ERROR: Pipe client had an error. Code: {0}", e.Message); } } catch { } pipeServer.Close(); } }).Start(); }
public static void SendCommand(string arg) { try { var pipeClient = new NamedPipeClientStream(".", "ShotrInputHandler", PipeDirection.InOut, PipeOptions.None); Console.WriteLine("[PIPE CLIENT] Connecting to pipe server.\n"); pipeClient.Connect(2000); Console.WriteLine("[PIPE CLIENT] Sending request to pipe server."); var ss = new StreamString(pipeClient); ss.WriteString(arg); Console.WriteLine("[PIPE CLIENT] Sent request to pipe server!"); pipeClient.Close(); } catch (Exception ex) { //Shotr isn't running. Re-run it? Console.WriteLine("[PIPE CLIENT] Sent request to pipe server!"); MessageBox.Show($"Error when trying to show other instance of Shotr: {ex}", "Can't show Shotr!", MessageBoxButtons.OK, MessageBoxIcon.Error); //Process.Start(Assembly.GetExecutingAssembly().Location); //Thread.Sleep(5000); //SendCommand(arg); } }