开发者

Run delegate method with BeginInvoke

开发者 https://www.devze.com 2023-01-22 04:43 出处:网络
In my class I have a static method public static void DoWork(开发者_StackOverflowint param) ... I want to run that method like:

In my class I have a static method

public static void DoWork(开发者_StackOverflowint param) ...

I want to run that method like:

Form.BeginInvoke(DoWork, param);

Is this operation possible?

I tried 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 without defining a delegate object (private delegate void DoWorkDelegate(int param))?


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);});
0

精彩评论

暂无评论...
验证码 换一张
取 消