Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My problem is that I cant send my custom object to OutputDataReceived method. Details:

I have a class called Material which includes properties such as URL, Title, Description etc.

Now I start a Process. My goal is to take Title by using URL. I mean:

(pseudo code)
Material m=new Material(URL);
Process proc=new Process();
proc.startInfo.FileName = youtube_dl;
proc.startInfo.Arguments="--get-title "+m.URL;  // get title using url
proc.OutputDataReceived += OutputDataReceived;

Till now, everything is ok. I can get data by using e.Data in
void OutputDataReceived(object sender, DataReceivedEventArgs e) method.

Problem is that I want to fill my Material object with this data like so:

But I cant pass any argument because eventhandler arguments are only object and DataReceivedEventArgs. So how should I do this without using a global variable.

What I have tried:

Nothing much, I tried to put my custom data into object paramter of handler but I failed. I'm stuck.
Updated 23-Jan-23 8:14am

1 solution

Ok I did it on my own:

Material m=new Material(URL);
process.OutputDataReceived +=(Object _sender, DataReceivedEventArgs _args)=>DoSomething(m, _sender, _args);

 public void DoSomething(Material mt,object sender, DataReceivedEventArgs e)
    MessageBox.Show("Captured Data:" + e.Data);
    MessageBox.Show("My Data:" + mt.Info);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900