tags:

views:

68

answers:

1

Hello, I would like to know about the Invoke(delegate) method. I do not understand why I do not need to specify arguments. What if I need them supply..Hopefully below you better understand what I mean. Thank you

 EventHandler a = new EventHandler(this.A);
            Invoke(a); //where doest it take the arguments from?

            a(); //does not work, missing arguments
+5  A: 

Because Invoke is meant to be used on Windows Forms, and the pattern for events used here is well specified, the Invoke method can make an educated guess. In fact, it is documented on MSDN exactly what it does, if you try to invoke an EventHandler without parameters using Invoke:

The delegate can be an instance of EventHandler, in which case the sender parameter will contain this control, and the event parameter will contain EventArgs.Empty.

You can, and should, use the overload of Invoke that allows you to specifiy the parameteres of your delegate, in order to make it more explicit what is going on. Also, calling Invoke without the parameters array will only work for delegates of type EventHandler (or, of course, delegates that does not take any parameters in the first place).

driis