views:

34

answers:

1

Hi,

In my class I have a staic method publis static void DoWork(int param) ... I want to run that method like:

Form.BeginInwoke(DoWork, param);

Is that possible? I tryed with the MethodInvoker class ... but I don't want to define the method body inline. Is there any generic delegate? Or do you know any other way of calling this ... widout defining a delegate object ( private delegate void DoWorkDelegate(int param))

Thanks, Radu

+3  A: 

You should be able to use:

form.BeginInvoke((Action<int>)DoWork, param);

As a side note, MethodInvoker has the advantage of special handling - it can invoke that one with typed-invoke, rather than reflection-invoke - and perhaps more importantly offers checking of the args ahead of time; personally I would just use:

form.BeginInvoke((MethodInvoker)delegate {DoWork(param);});
Marc Gravell
Note: .NET 3.5 or higher
Onkelborg
@Onkelborg - no; actually this *particular* one is 2.0: http://msdn.microsoft.com/en-us/library/018hxwa8(v=VS.80).aspx
Marc Gravell
You might want to mention that BeginInvoke is an instance method.
Hans Passant
@Marc Gravell: Ah, didn't know that :)
Onkelborg