异步模式下的StandardOutput流出现问题 [英] Problem with StandardOutput stream in async mode
问题描述
我查看了Process类看看有没有什么可以对我有用,等待流为空,但我发现只有同步模式。你可以帮助吗?
Thanx。
遇到类似的问题:我的应用程序本质上是一些cygwin应用程序的前端,有时应用程序在通过OutputDataReceived事件接收到的所有数据之前退出,我丢失数据。
我的修复/黑客是在进程对象消失之前在输出AsyncStreamReader上调用WaitUtilEOF(WaitUtilEOF在.NET框架内部类上必须使用反射)。这将导致调用方阻塞,直到所有的异步数据已经通过OutputDataReceived刷新。我不知道是否直接解决您的问题,但可能有助于...
private static void WaitUntilAsyncStreamReachesEndOfFile进程进程,字符串字段)
{
FieldInfo asyncStreamReaderField = typeof(Process).GetField(field,BindingFlags.NonPublic | BindingFlags.Instance);
对象asyncStreamReader = asyncStreamReaderField.GetValue(process);
输入asyncStreamReaderType = asyncStreamReader.GetType();
MethodInfo waitUtilEofMethod = asyncStreamReaderType.GetMethod(@WaitUtilEOF,BindingFlags.NonPublic | BindingFlags.Instance);
object [] empty = new object [] {};
waitUtilEofMethod.Invoke(asyncStreamReader,empty);
}
我打电话给这样:
WaitUntilAsyncStreamReachesEndOfFile(process,output);
祝你好运!
I have a program that launches command line processes in async mode, using BeginOutputReadLine. My problem is that the .Exited event is triggered when there is still some .OutputDataReceived events being triggered. What I do in my .Exited event must happen only once all my .OutputDataReceived events are done, or I'll be missing some output.
I looked in the Process class to see if anything could be useful to me, as to wait for the stream to be empty, but all I find is for sync mode only. Can any of you help?
Thanx.
I've run into a similar problem: my app's essentially a frontend to some cygwin apps and sometimes the app exits before all of the data's received via the OutputDataReceived event, and I lose data.
My fix / hack is to call WaitUtilEOF on the output AsyncStreamReader before the process object disappears (have to use reflection since WaitUtilEOF is on an .NET framework internal class). This causes the caller to block until all of the async data has been flushed through OutputDataReceived. I'm not sure if it'll directly solve your problem, but it might help...
private static void WaitUntilAsyncStreamReachesEndOfFile(Process process, string field)
{
FieldInfo asyncStreamReaderField = typeof(Process).GetField(field, BindingFlags.NonPublic | BindingFlags.Instance);
object asyncStreamReader = asyncStreamReaderField.GetValue(process);
Type asyncStreamReaderType = asyncStreamReader.GetType();
MethodInfo waitUtilEofMethod = asyncStreamReaderType.GetMethod(@"WaitUtilEOF", BindingFlags.NonPublic | BindingFlags.Instance);
object[] empty = new object[] { };
waitUtilEofMethod.Invoke(asyncStreamReader, empty);
}
And i'm calling it like this:
WaitUntilAsyncStreamReachesEndOfFile(process, "output");
Good luck!
这篇关于异步模式下的StandardOutput流出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!