将匿名委托传递给线程...为什么这有效?
在我的程序中,我们将需要在四个线程中查看的大量数据分开。
Thread one = new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[0], param2, param3, param4, param5); });
Thread two = new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[1], param2, param3, param4, param5); });
Thread three = new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[2], param2, param3, param4, param5); });
Thread four= new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[3], param2, param3, param4, param5); });
我们的编码标准要求我们符合 StyleCop,而 StyleCop 要求满足以下要求:
SA1410:从匿名方法中删除括号,因为委托的参数列表为空。
这样做会给我这个编译器错误:
以下方法或属性之间的调用不明确:“System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)”和“System.Threading.Thread.Thread(System.Threading.ThreadStart)”
我已经研究过ThreadStart 和 ParameterizedThreadStart 对象,我只是不知道如何使用这些对象中的任何一个来完成我需要做的事情。
我的问题:匿名代表如何工作?他们编译成什么?最后,我将不得不在没有匿名代表的情况下完成这项工作,但我不知道从哪里开始。
帮助
感谢寻求者的
In my program, we split up a large amount of data that needs to be looked over across four threads.
Thread one = new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[0], param2, param3, param4, param5); });
Thread two = new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[1], param2, param3, param4, param5); });
Thread three = new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[2], param2, param3, param4, param5); });
Thread four= new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[3], param2, param3, param4, param5); });
Our coding standards require that we are StyleCop compliant, as it were, and StyleCop demands the following:
SA1410: Remove the parenthesis from the anonymous method, since the delegate's parameter list is empty.
Doing that gives me this compiler error:
The call is ambiguous between the following methods or properties: 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)' and 'System.Threading.Thread.Thread(System.Threading.ThreadStart)'
I've looked into the ThreadStart and ParameterizedThreadStart objects and I just can't figure out how to get what I need done with either of those objects.
My question: how do the anonymous delegates work? What do they compile down to? In the end, I will have to get this working without the anonymous delegates, but I don't know where to begin.
Thanks for the help,
Seeker
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有两个选择:
new ThreadStart(delegate { ... })
说明:
不带括号的匿名方法 (
delegate { ... }< /code>) 有一个隐式参数列表。编译器将为其提供匹配其所用作的委托所需的任何参数。 (您的代码看不到参数)
当编写不使用其参数的匿名事件处理程序时,这非常有用;它使您无需输入
delegate(object sender, EventArgs e) { ... }
。但是,在调用
Thread
构造函数时,有两种不同的重载需要两种委托。编译器无法知道您正在尝试创建哪种委托类型,因为您没有指定参数列表。
You have two options:
new ThreadStart(delegate { ... })
Explanation:
An anonymous method without parentheses (
delegate { ... }
) has an implicit parameter list. The compiler will give it whatever parameters are needed to match the delegate it's being used as. (Your code cannot see the parameters)This is very useful when writing anonymous event handlers that don't use their parameters; it saves you from typing
delegate(object sender, EventArgs e) { ... }
.However, when calling the
Thread
constructor, there are two different overloads that take two kinds of delegates.The compiler has no way of knowing which delegate type you're trying to create, since you didn't specify a parameter list.
StyleCop 很蠢。
StyleCop 暗示这两种语法具有相同的含义。 这是完全错误的。省略委托中的括号不意味着“这不需要参数”。它的意思是“为我填写任何参数,因为无论如何我都不会使用它们。”
由于 Thread:.ctor 有两个不同的签名可用,每个签名都采用不同的委托类型,因此编译器无法知道选择哪一个,因为使用任何一个都可以。添加括号会强制编译器选择 ThreadStart 变体,因为它是委托类型与匿名方法兼容的唯一签名。
如果你想让 StyleCop 高兴并且编译你的代码,这是一个选择:
这是另一个选择:
但我的建议是向 StyleCop 作者提出这个荒谬的规则。
StyleCop is dumb.
StyleCop is implying that the two syntaxes have the same meaning. This is completely wrong. Omitting the parentheses from a delegate does not mean "this takes no arguments." It means "fill in any arguments for me, because I won't be using them anyway."
Since two different signatures of
Thread:.ctor
are available, each taking a different delegate type, the compiler cannot know which one to pick, since it would be just fine with either one. Adding the parentheses forces the compiler to pick theThreadStart
variant, since it is the only signature with a delegate type compatible with your anonymous method.If you want to make StyleCop happy and have your code compile, this is one option:
This is another:
But my suggestion would be to LART the StyleCop authors for introducing this ridiculous rule.
您可以使用 lambda 表达式代替
delegate
关键字:You could use a lambda expression instead of the
delegate
keyword: