使用流程标准输出不断更新表单

发布于 2024-08-03 09:50:26 字数 83 浏览 1 评论 0原文

我有一个 C# 表单,其中有一个文本框,需要在 exe 仍在运行时不断更新 exe 的输出。我知道如何在 exe 完成后更新它,但它是我需要的不断更新。

I have a C# form that has a text box that needs to constantly update with the output from an exe while the exe is still running. I know how to update it after the exe has finished but its the constant updating that i need.

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

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

发布评论

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

评论(3

飞烟轻若梦 2024-08-10 09:50:26

您有启动 exe 的进程:

// Normal creation and initialization of process - additionally:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += ProcessOnOutputDataReceived;
process.Start();
process.BeginOutputReadLine();

和处理程序:

private void ProcessOnOutputDataReceived( object sender, DataReceivedEventArgs args )
{
    // use args.Data
}

编辑:
我忘记了 process.StartInfo.UseShellExecute = false; :/

编辑:
事实证明,exe 没有刷新输出(参见评论)

You have your process which starts the exe:

// Normal creation and initialization of process - additionally:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += ProcessOnOutputDataReceived;
process.Start();
process.BeginOutputReadLine();

and the handler:

private void ProcessOnOutputDataReceived( object sender, DataReceivedEventArgs args )
{
    // use args.Data
}

EDIT:
I forgot process.StartInfo.UseShellExecute = false; :/

EDIT:
It turned out, that the exe did not flush the output (see comments)

弃爱 2024-08-10 09:50:26

实现此目的的一种方法是使用远程处理 - 换句话说,您将轮询正在运行的可执行文件中的众所周知的方法来检索值并基于此更新文本框中的值。 Google .net 远程处理某些示例。

或者(如果您使用 .NET3 及以上版本),您可以使用 WCF 和 Tcp 或 Peer 2 Peer 绑定之类的东西。如果不了解更多关于您的架构的信息,我对此无话可说。

One way to do this would be to use remoting - in other words, you would poll a well known method in the running executable to retrieve the value and update the value in your textbox based on this. Google .net remoting for some samples.

Alternatively (if you're using .NET3 and on), you could use WCF and something like Tcp or Peer 2 Peer bindings. Without knowing more about your architecture, there's not much more I could say on this.

羁拥 2024-08-10 09:50:26

当您说 exe 时,您是在谈论使用进程启动来运行控制台应用程序吗?

如果是这样,那么您必须获取控制台应用程序的输出,请参阅此 问题

这是发送输入,有一个读取输出的示例。

另一种选择是让调用类连接到 Windows 窗体中的事件。它将调用该事件将导致表单更新文本框。

When you say exe are you talking about running a console application by using process start?

If so then you have to get the output of the console app, see this question.

This is sending input there is an example to read output.

The other option is to have the calling class hook up to an event from your windows form. It will call the event will will cause the form to update the text box.

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