All pastes #1958206 Raw Edit

ParentProcess

public text v1 · immutable
#1958206 ·published 2010-10-09 16:06 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;
using System.IO.Pipes;

namespace ParentProcess
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Process pipeClient;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            pipeClient = new Process();

            pipeClient.StartInfo.FileName = Environment.CurrentDirectory + "\\ChildProcess.exe";

            pipeClient.Exited += new EventHandler(pipeClient_Exited);
            pipeClient.Start();
        }

        void pipeClient_Exited(object sender, EventArgs e)
        {
            using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable))
            {
                // Show that anonymous pipes do not support Message mode.
                try
                {
                    MessageBox.Show("[SERVER] Setting ReadMode to \"Message\".");
                    pipeServer.ReadMode = PipeTransmissionMode.Message;
                }
                catch (NotSupportedException ex)
                {
                    MessageBox.Show("[SERVER] Exception:\n    {0}", ex.Message);
                }

                MessageBox.Show("[SERVER] Current TransmissionMode: {0}.", pipeServer.TransmissionMode.ToString());

                // Pass the client process a handle to the server.
                pipeClient.StartInfo.Arguments =
                    pipeServer.GetClientHandleAsString();
                pipeClient.StartInfo.UseShellExecute = false;
                pipeClient.Start();

                pipeServer.DisposeLocalCopyOfClientHandle();

                try
                {
                    // Read user input and send that to the client process.
                    using (StreamWriter sw = new StreamWriter(pipeServer))
                    {
                        sw.AutoFlush = true;
                        // Send a 'sync message' and wait for client to receive it.
                        sw.WriteLine("SYNC");
                        pipeServer.WaitForPipeDrain();
                        // Send the console input to the client process.
                        MessageBox.Show("[SERVER] Enter text: ");
                        sw.WriteLine(Console.ReadLine());
                    }
                }
                // Catch the IOException that is raised if the pipe is broken
                // or disconnected.
                catch (IOException ex)
                {
                    MessageBox.Show("[SERVER] Error: {0}", ex.Message);
                }
            }

            pipeClient.WaitForExit();
            pipeClient.Close();
            MessageBox.Show("[SERVER] Client quit. Server terminating.");
        }
    }
}