C# - 覆盖事件处理程序 - 添加参数 [英] C# - Overriding an Event Handler - Adding a parameter
问题描述
我正在使用 System.Diagnostics.Process
类来执行命令行程序。
I'm using the System.Diagnostics.Process
class to execute a command line program.
我我使用 OutputDataReceived
方法将输出重定向到我自己的方法。
I am using the OutputDataReceived
method to redirect the output to my own method.
pr.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
pr.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
但是,我有多个Threads运行这个cmd程序的多个实例。我想要做的是能够识别输出数据来自哪个流程实例 - 理想情况下,一个包含名称的字符串。 (每个进程在GUI上都有自己的进度条,我创建另一个事件来将输出传递给GUI,因此,我需要知道数据来自哪个进程来更新进度条)。
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).
我开始尝试:
public override delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e, string processName);
然后我意识到我必须覆盖 OutputDataReceived
方法在Process类中。这反过来意味着我必须创建一个继承 System.Diagnostics.Process
的自定义类,并有一个接受字符串参数的方法,以便 OutputDataReceived
事件可以将流程实例名称(字符串)传递给我覆盖的 DataReceivedEventHandler
。
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?
推荐答案
你不能只使用 / code>对象传回并检查它正在运行的进程?
Can you not just use the sender
object passed back and check which process it is running?
这篇关于C# - 覆盖事件处理程序 - 添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!