匿名方法作为参数传递给BeginInvoke的? [英] Anonymous method as parameter to BeginInvoke?
问题描述
为什么你不能传递一个匿名方法作为参数传递给的BeginInvoke
的方法?我有以下代码:
<预类=郎-CS prettyprint-覆盖>
私人委托无效CfgMnMnuDlg( DIServer SVR);
私人无效ConfigureMainMenu(DIServer服务器)
{
的MenuStrip mnMnu = PresenterView.MainMenu;
如果(mnMnu.InvokeRequired)
{
mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu,
新的对象[] {服务器});
}
,否则
{
//做实际的工作在这里
}
}
我试图避免申报委托。为什么我不能写类似下面的呢?还是可以的我,我只是想不出正确的语法?下面当前生成:
参数类型为匿名方法'不是分配给参数类型'System.Delegate'
块引用>
好吧,这是正确的,当然,但有一些其他的语法,我可以用它来做到这一点(避免为了使用声明一个单独的委托
的BeginInvoke()
?
(能够做到这会适合整齐的使用匿名方法/ lamdas的概念代替它的工作原理是如此工整其他地方明确的代表。)
<预类=郎-CS prettyprint-覆盖>私人无效ConfigureMainMenu(DIServer服务器)
{
的MenuStrip mnMnu = PresenterView.MainMenu;
如果(mnMnu.InvokeRequired)
{
mnMnu.BeginInvoke(/ /通匿名方法,而不是
代表(DIServer SVR){ConfigureMainMenu(服务器);}?,
新的对象[] {服务器});
}
,否则
{
//做实际的工作在这里
}
}
解决方案试试这个:
<预类=郎-CS prettyprint-覆盖>control.BeginInvoke((MethodInvoker)代表{/ *方法的详细信息* /});
或者
<预类=郎-CS prettyprint-覆盖>私人无效ConfigureMainMenu(DIServer服务器)
{
如果(control.InvokeRequired)
{
control.BeginInvoke(新动作< DIServer>(ConfigureMainMenu),服务器);
}
,否则
{
/ *做的工作* /
}
}
或者
<预类=郎-CS prettyprint-覆盖>私人无效ConfigureMainMenu(DIServer服务器)
{
的MenuStrip mnMnu = PresenterView.MainMenu;
如果(mnMnu.InvokeRequired)
{
//私有变量
_methodInvoker =新MethodInvoker((动作)(()=> ConfigureMainMenu(服务器)));
_methodInvoker.BeginInvoke(新的AsyncCallback(ProcessEnded),NULL); //调用_methodInvoker.EndInvoke在ProcessEnded
}
,否则
{
/ *做的工作* /
}
}
Why can't you pass an anonymous method as a parameter to the
BeginInvoke
method? I have the following code:private delegate void CfgMnMnuDlg(DIServer svr); private void ConfigureMainMenu(DIServer server,) { MenuStrip mnMnu = PresenterView.MainMenu; if (mnMnu.InvokeRequired) { mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu, new object[] { server}); } else { // Do actual work here } }
I'm trying to avoid declaring the delegate. Why can't I write something like the below instead? Or can I, and I just can't figure out the correct syntax? The below currently generates an:
Argument type 'Anonymous method' is not assignable to parameter type 'System.Delegate'
Ok, that's right of course, but is there some other syntax I can use to do this (avoid having to declare a separate delegate in order to use
BeginInvoke()
?(Being able to do this would fit in neatly with the concept of using anon methods/lamdas in place of explicit delegates which works so cleanly everywhere else.)
private void ConfigureMainMenu(DIServer server,) { MenuStrip mnMnu = PresenterView.MainMenu; if (mnMnu.InvokeRequired) { mnMnu.BeginInvoke( // pass anonymous method instead ? delegate(DIServer svr) { ConfigureMainMenu(server);}, new object[] { server}); } else { // Do actual work here } }
解决方案Try this:
control.BeginInvoke((MethodInvoker) delegate { /* method details */ });
Or:
private void ConfigureMainMenu(DIServer server) { if (control.InvokeRequired) { control.BeginInvoke(new Action<DIServer >(ConfigureMainMenu), server); } else { /* do work */ } }
Or:
private void ConfigureMainMenu(DIServer server) { MenuStrip mnMnu = PresenterView.MainMenu; if (mnMnu.InvokeRequired) { // Private variable _methodInvoker = new MethodInvoker((Action)(() => ConfigureMainMenu(server))); _methodInvoker.BeginInvoke(new AsyncCallback(ProcessEnded), null); // Call _methodInvoker.EndInvoke in ProcessEnded } else { /* do work */ } }
这篇关于匿名方法作为参数传递给BeginInvoke的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!