First off, why don't you use the Process.StandardInput
-property directly as your target, like
var process = new Process
{
// all your init stuff
};
var xmlSerializer = new XmlSerializer(data.GetType());
var xmlwriter = XmlWriter.Create(process.StandardInput, xmlWriterSettings);
xmlSerializer.Serialize(xmlwriter, data);
Otherwise, the msdn-entry gives a clear howto for using Process.BeginOutputReadLine()
, which you can remodel to
var autoResetEvent = new AutoResetEvent(false); // this mutex acts as our bouncer for the reading-part
var process = new Process
{
// all your init stuff
};
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, args) => {
// TODO you could read the content here with args.Data
autoResetEvent.Set();
};
process.Start();
using (var memoryStream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(memoryStream))
{
var xmlSerializer = new XmlSerializer(data.GetType());
var xmlwriter = XmlWriter.Create(streamWriter, xmlWriterSettings);
xmlSerializer.Serialize(xmlwriter, data);
}
memoryStream.Position = 0;
using (var streamReader = new StreamReader(memoryStream))
{
while (!streamReader.EndOfStream)
{
var line = streamReader.ReadLine();
process.StandardInput.WriteLine(line);
Console.WriteLine(line);
process.BeginOutputReadLine();
autoResetEvent.WaitOne();
}
}
}
// TODO closing the process.StandardInput, exiting process, ...
Anyway - I know this should be a comment - is there a specific reason why you are waiting for your process to write something?
The StandardOutput stream can be read synchronously or asynchronously.
Methods such as Read, ReadLine, and ReadToEnd perform synchronous read
operations on the output stream of the process. These synchronous read
operations do not complete until the associated Process writes to its
StandardOutput stream, or closes the stream. In contrast,
BeginOutputReadLine starts asynchronous read operations on the
StandardOutput stream. This method enables a designated event handler
for the stream output and immediately returns to the caller, which can
perform other work while the stream output is directed to the event
handler.
Which means, that if your process does not write anything (and you are waiting), you are spinning for response endlessly ...
EDIT
You should additionally add a handler to Process.ErrorDataReceived
like
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += (sender, args) => {
// TODO do something with the response of args.Data
autoResetEvent.Set();
};
and
while (!streamReader.EndOfStream)
{
var line = streamReader.ReadLine();
process.StandardInput.WriteLine(line);
Console.WriteLine(line);
process.BeginOutputReadLine();
process.BeginErrorReadLine();
autoResetEvent.WaitOne();
}
to handle error-cases as well (whatever that may mean).
xs.Serialize(p.StandardInput, data)
?lock
- so it is not fully obvious, to me, how there should be a hang (which could be described as a deadlock)...