2

I feel like this is really basic, but I'm having trouble with this issue. I'm using a Process object and subscribing to a DataReceivedEventHandler. This event handler then delegates to another method, in this case "DoSomething", and the signature for the arguments is (Object sender, DataReceivedEventArgs args). What I need to do, is extend something, or provide something that will pass in additional information. Here is my current code:

// an object of some type
MyCustomObject obj = new MyCustomObject();

// set up obj and Process

process.OutputDataReceived += new DataReceivedEventHandler(DoSomething);

public void DoSomething(Object sender, DataReceivedArgs args)
{
    // do some stuff, however, I need the "obj" object passed in for work
}

I feel like this is really trivial, but not sure how to proceed. I've read about subclassing the "EventArgs," but not sure how that will help, or how to even change the signature of "DoSomething" to accept a DataReceivedArgsExtended parameter, since the DataReceivedEventHandler is expecting a method with a DataReceivedArgs

5StringRyan
  • 3,604
  • 5
  • 46
  • 69
  • What type of additional info do you need to pass? DataReceivedArgs just has the Data property which contains the output stream. – Michael D. Irizarry Oct 13 '11 at 16:00
  • Yes it does, however I would like to write this output to a winforms richtextbox that resides on a tab page. I would like to pass in the tabpage. I would like to create multiple processes running, and would like to pass in the TabPage, so that the output will only output here. Right now I have a hardcoded "RichTextBox" inside the "DoSomething" method. – 5StringRyan Oct 13 '11 at 16:05

2 Answers2

4

Yes you can extend your DataReceivedArgs to DataReceivedArgsExtended, but remeber cast it into event handler method. For example:

public class MyObject
{
    public event EventHandler<MyEventArgs> OnFire;

    public void Fire()
    {
        if( OnFire != null )
        {
            //var e = new MyEventArgs { X=2 };

            var e = new MyEventArgsNew { X = 3, Y = 4 };

            OnFire( this, e );
        }
    }
}

public class MyEventArgs : EventArgs
{
    public int X { get; set; }
}

public class MyEventArgsNew : MyEventArgs
{
    public int Y { get; set; }
}

static void Main( string[] args )
{
        var obj = new MyObject();

        obj.OnFire += new EventHandler<MyEventArgs>( obj_OnFire );

        obj.Fire();

        Console.ReadLine();
 }

 static void obj_OnFire( object sender, MyEventArgs e )
 {
        var e2 = (MyEventArgsNew)e;

        Console.WriteLine( e2.X );
        Console.WriteLine( e2.Y );
 }
ADIMO
  • 1,107
  • 12
  • 24
  • If a new event args is created (for example in my situation DataReceivedArgsExtended), would I have to extend the `Process` class to us this args? – 5StringRyan Oct 13 '11 at 18:38
  • Yes, because only your Process class know the args information to pass to the event. – ADIMO Oct 13 '11 at 21:08
0

Your question is already answered in the following questions:

C# passing extra parameters to an event handler?

How can I pass addition local object variable to my event handler? [duplicate]

In your case you need to use an anonymous delegate function or a lambda expression in the same scope and from within it call your own function, containing the event handle parameters and your additional ones:

// an object of some type
MyCustomObject obj = new MyCustomObject();

// set up obj and Process

process.OutputDataReceived +=
    (Object _sender, DataReceivedArgs _args) =>
        DoSomething(obj, _sender, _args);

public void DoSomething(Process process, Object sender, DataReceivedArgs args)
{
    // do some stuff
}
Community
  • 1
  • 1
Bonnev
  • 947
  • 2
  • 9
  • 29