C# - Overriding an Event Handler - Adding a parameter

霸气de小男生 提交于 2019-12-06 10:10:58

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!