Skip to content

Instantly share code, notes, and snippets.

@joymon
Created July 20, 2015 16:56
Show Gist options
  • Save joymon/ca9dfd291ce6009ee020 to your computer and use it in GitHub Desktop.
Save joymon/ca9dfd291ce6009ee020 to your computer and use it in GitHub Desktop.
IPC using StdIn and Out Soruce
internal class ProcessStarter
{
internal void StartExeToProcessMessage(string pathToExe, CustomQueueMessage customMsg)
{
string serializedMsg = JsonConvert.SerializeObject(customMsg);
using (Process p = GetProcessAfterStarting(pathToExe))
{
p.StandardInput.WriteLine(serializedMsg);
p.OutputDataReceived += (sender, args) => Console.WriteLine("Data receied from child exe - {0}", args.Data);
p.EnableRaisingEvents = true;
p.BeginOutputReadLine();
p.WaitForExit(); //Make sure it wait until child exit
p.CancelOutputRead();
}
}
private static Process GetProcessAfterStarting(string pathToExe)
{
Process process = Process.Start(new ProcessStartInfo()
{
FileName = pathToExe,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false,
Arguments = "-process"//Change based on scenario.
});
return process;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment