C# - 重写事件处理程序 - 添加参数

发布于 2024-09-28 10:35:03 字数 843 浏览 3 评论 0原文

我正在使用 System.Diagnostics.Process 类来执行命令行程序。

我正在使用 OutputDataReceived 方法将输出重定向到我自己的方法。

pr.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
pr.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);

但是,我有多个线程运行此 cmd 程序的多个实例。我想要做的是能够识别输出数据来自哪个流程实例 - 理想情况下是包含名称的字符串。 (每个进程在 GUI 上都有自己的进度条。我创建另一个事件将输出传递到 GUI,因此,我需要知道数据来自哪个进程来更新其进度条)。

我开始尝试:

public override delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e, string processName);

然后我意识到我必须重写 Process 类中的 OutputDataReceived 方法。这反过来意味着我必须创建一个继承 System.Diagnostics.Process 的自定义类,并有一个接受字符串参数的方法,以便 OutputDataReceived 事件可以传递流程实例名称(字符串)到我重写的 DataReceivedEventHandler 。

问题的目的是获得一些关于如何进行的意见。我的提议似乎是实现我想要的目标的正确方法吗?或者,有更好的方法吗?

I'm using the System.Diagnostics.Process class to execute a command line program.

I am using the OutputDataReceived method to redirect the output to my own method.

pr.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
pr.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);

However, I have multiple Threads running multiple instances of this cmd program. What I want to do is to be able to identify which process instance the output data came from - ideally, a string containing a name. (Each process has it's own progress bar on a GUI. I create another event to pass the output to the GUI, thus, I need to know which process the data came from to update their progress bar).

I started to experiment with:

public override delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e, string processName);

Then I realised that I would have to override the OutputDataReceived method inside the Process class. Which in turn would mean I have to create a custom class that inherits System.Diagnostics.Process, and have a method that accepts a string argument so the OutputDataReceived event can pass the process instance name (string) to my overridden DataReceivedEventHandler.

The purpose of the question is to get some opinions on how to proceed. Does what I propose seem the right way to accomplish what I want? Or, is there a better way to do it?

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

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

发布评论

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

评论(3

妄断弥空 2024-10-05 10:35:03

除了使用 sender 的现有答案之外,您还可以使用 lambda 表达式(或匿名方法)来简化此操作:

pr.OutputDataReceived += (sender, args) => HandleData(pr, args);

的签名

void HandleData(Process process, DataReceivedEventArgs e)

其中 HandleData 将具有匿名函数 将事件订阅时本地已知的信息传播到需要处理事件的代码的一种非常方便的方法。

Aside from the existing answers of using the sender, you could also use lambda expressions (or anonymous method) to make this simpler:

pr.OutputDataReceived += (sender, args) => HandleData(pr, args);

where HandleData would have a signature of

void HandleData(Process process, DataReceivedEventArgs e)

Anonymous functions are a very handy way of propagating information which is known locally at event subscription time to code which needs to handle the event.

梦里的微风 2024-10-05 10:35:03

您不能只使用传回的 sender 对象并检查它正在运行哪个进程吗?

Can you not just use the sender object passed back and check which process it is running?

浅语花开 2024-10-05 10:35:03

您可以将 sender 参数类型转换为 Process 对象(代码片段中的 pr

You can typecast the sender parameter to the Process object (pr in your code snippet)

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