异步模式下标准输出流的问题

发布于 2024-08-22 13:34:55 字数 252 浏览 10 评论 0原文

我有一个程序,使用 BeginOutputReadLine 以异步模式启动命令行进程。我的问题是,当仍有一些 .OutputDataReceived 事件被触发时,.Exited 事件就会被触发。我在 .Exited 事件中所做的操作必须仅在所有 .OutputDataReceived 事件完成后才会发生,否则我将丢失一些输出。

我查看了 Process 类,看看是否有任何内容对我有用,例如等待流为空,但我发现的只是同步模式。你们中有人能帮忙吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-08-29 13:34:55

我遇到了类似的问题:我的应用程序本质上是某些 cygwin 应用程序的前端,有时应用程序会在通过 OutputDataReceived 事件接收到所有数据之前退出,并且我会丢失数据。

我的修复/破解是在进程对象消失之前在输出 AsyncStreamReader 上调用 WaitUtilEOF(必须使用反射,因为 WaitUtilEOF 位于 .NET 框架内部类上)。这会导致调用者阻塞,直到所有异步数据都通过 OutputDataReceived 刷新。我不确定它是否会直接解决你的问题,但它可能会有所帮助......

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);
}

我这样称呼它:

WaitUntilAsyncStreamReachesEndOfFile(process, "output");

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文