Created
July 20, 2015 16:56
-
-
Save joymon/ca9dfd291ce6009ee020 to your computer and use it in GitHub Desktop.
IPC using StdIn and Out Soruce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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