Created
June 6, 2016 08:30
-
-
Save deostroll/b5ad3d35301324d244af23a95e8cfadb to your computer and use it in GitHub Desktop.
correctly redirecting i/o to a process started via a c#
This file contains 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
var rl = require('readline'); | |
var fs = require('fs'); | |
var util = require('util'); | |
var log = fs.createWriteStream('trace.log'); | |
var out = process.stdout; | |
function format(dt) { | |
var h = dt.getHours(), | |
m = dt.getMinutes(), | |
s = dt.getSeconds(); | |
return [h, m, s].join(':'); | |
} | |
console.log = function () { | |
log.write(util.format.apply(null, arguments) + '\r\n'); | |
out.write(util.format.apply(null, arguments) + '\r\n'); | |
}; | |
var stdinParser = rl.createInterface({ | |
input: process.stdin | |
}); | |
var count = 0; | |
stdinParser.on('line', function (line) { | |
if (line.length) count++; | |
}); | |
stdinParser.on('close', function () { | |
var dt = new Date(); | |
console.log('Count:', count); | |
console.log(format(dt)); | |
}); | |
console.log('started...'); |
This file contains 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
using System; | |
using System.Diagnostics; | |
using System.IO; | |
namespace caStreamedInput | |
{ | |
class Program | |
{ | |
static string Ask() | |
{ | |
Console.Write("Input file path:"); | |
return Console.ReadLine(); | |
} | |
static void Main(string[] args) | |
{ | |
var file = @"D:\misc\capture635985621180258666.txt"; | |
var proc = new Process(); | |
string data = string.Empty; | |
//ManualResetEvent evt = new ManualResetEvent(false); | |
proc.OutputDataReceived += (sndr, resp) => | |
{ | |
Console.WriteLine("Received: {0}", resp.Data); | |
}; | |
proc.ErrorDataReceived += (sndr, errEvt) => | |
{ | |
Console.WriteLine("ERR: {0}", errEvt.Data); | |
}; | |
var si = new ProcessStartInfo(@"c:\Program Files\nodejs\node.exe", @"app.js"); | |
si.WorkingDirectory = @"C:\Users\arun_jayapal\Documents\Visual Studio 2015\Projects\StreamedInputDemo\njsConsoleApp"; | |
si.RedirectStandardInput = true; | |
si.RedirectStandardOutput = true; | |
si.RedirectStandardError = true; | |
si.UseShellExecute = false; | |
proc.StartInfo = si; | |
proc.Start(); | |
//proc.StandardInput.Write(File.ReadAllText(file)); | |
//proc.StandardInput.Close(); | |
File.OpenRead(file).CopyToAsync(proc.StandardInput.BaseStream).ContinueWith((_) => | |
{ | |
proc.StandardInput.Close(); | |
}); | |
Console.WriteLine("Waiting..."); | |
proc.BeginOutputReadLine(); | |
proc.WaitForExit(); | |
proc.Close(); | |
Console.WriteLine("Data: {0}", data); | |
Console.WriteLine("end"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment