/// <summary> /// Tests the binary formatter communication method. /// </summary> /// <param name="message">Message to transmit</param> /// <param name="launcherExeName">Name of EXE (necessary to allow multiple tests to run concurrently).</param> private void TestBinaryFormatter(string message, string launcherExeName) { var pipeNamePrefix = System.Guid.NewGuid().ToString("N"); var childProcessMainMethod = typeof(ChildProcess).GetMethod("Start"); var exePath = ExecutableGenerator.Instance.GenerateExecutable(childProcessMainMethod, ExecutableType.Default, launcherExeName); var proc = Process.Start(new ProcessStartInfo(exePath, pipeNamePrefix)); var client = new Proliferate.ProliferateClient(pipeNamePrefix); using (var cancelTokenSource = new System.Threading.CancellationTokenSource()) { client.StartChildPinger(cancelTokenSource.Token); using (var messenger = client.GetMessenger <Msg, Msg>()) { var resp = messenger.SendRequest(new Msg() { Message = message }); Assert.IsTrue(resp.Message == message); } } client.SendShutdown(); }
static void Main(string[] args) { //Make sure pipeNamePrefix is random so separate instances of your program don't talk to each other's child processes. var pipeNamePrefix = System.Guid.NewGuid().ToString("N"); var childProcessMainMethod = typeof(ChildProcess).GetMethod("Start"); var watch = System.Diagnostics.Stopwatch.StartNew(); var exePath = ExecutableGenerator.Instance.GenerateExecutable(childProcessMainMethod, ExecutableType.Default, "Launcher"); Console.WriteLine("Generating executable took: " + watch.Elapsed.ToString()); //The CreateNoWindow option reduces startup time of the child process. var startInfo = new System.Diagnostics.ProcessStartInfo(exePath, pipeNamePrefix) { CreateNoWindow = true, UseShellExecute = false }; watch.Restart(); var proc = System.Diagnostics.Process.Start(startInfo); var client = new Proliferate.ProliferateClient(pipeNamePrefix); using (var cancelTokenSource = new System.Threading.CancellationTokenSource()) { client.StartChildPinger(cancelTokenSource.Token); using (var streams = client.GetSendAndReceiveStreams()) { streams.OutgoingRequestStream.Close(); //If we don't read the response from the child process, there will be an IO exception //in the child process while it tries to write a response back. var buffer = new byte[1024]; var read = streams.IncomingResponseStream.Read(buffer, 0, buffer.Length); } Console.WriteLine("Time from process start until response received: " + watch.Elapsed.ToString()); while (true) { Console.WriteLine("Enter a string to send to the child process or press enter to end the program..."); var request = Console.ReadLine(); if (System.Text.RegularExpressions.Regex.IsMatch(request, @"^\r*\n*$")) { client.SendShutdown(); cancelTokenSource.Cancel(); return; } string response; var writerAndReader = client.GetRequestWriterAndResponseReader(); { writerAndReader.OutgoingRequestWriter.Write(request); writerAndReader.OutgoingRequestWriter.Close(); response = writerAndReader.IncomingResponseReader.ReadToEnd(); writerAndReader.IncomingResponseReader.Close(); } Console.WriteLine("Child process sent back: " + response); } } }